Guest User

Untitled

a guest
Dec 13th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import {viewFromComponent} from "viewUtilities";
  2.  
  3. class MyListComponent extends React.Component {
  4. render() {
  5. // serialized Backbone collection
  6. const {items = [] } this.props;
  7. // do stuff with items
  8. }
  9. }
  10.  
  11. class MySingleComponent extends React.component {
  12. render() {
  13. // serialized Backbone model, with whatever your real fields are
  14. const {a, b, c} = this.props;
  15. // do stuff with fields
  16. }
  17. }
  18.  
  19. const MyListView = viewFromComponent(MyListComponent, {
  20. observe : {
  21. // Every time one of these events is triggered, the collection will be serialized
  22. // and the HOC will call this.setState()
  23. collection : "add remove reset",
  24. },
  25. actions : {
  26. addNewItem(newItem) {
  27. // This function runs in the context of the HOC generated by viewFromComponent,
  28. // so `this` is actually the parent of your "real" component. This gives you
  29. // access to the model/collection instance to let you mutate them, while keeping
  30. // your real component unaware of Backbone entirely.
  31. // Yes, this is sorta-kinda mutating "props" in a way, but it's an incremental
  32. // step towards turning this into a component callback or Redux action creator.
  33. this.props.collection.add(newItem);
  34. }
  35. }
  36. });
  37.  
  38. const MySingleView = viewFromComponent(MySingleComponent, {
  39. observe : {
  40. model : "change"
  41. },
  42. actions : {
  43. setSomeField(newValue) {
  44. this.props.model.set("someField", someValue);
  45. }
  46. }
  47. }, {staticClassProp : 123}, {id : "myViewId", el : "#divToAttachTo"});
  48.  
  49. const collectionView = new MyListView({collection : someBackboneCollection});
  50.  
  51. const modelView = new MySingleView({model : someBackboneModel});
Add Comment
Please, Sign In to add comment