Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 2.26 KB  |  hits: 34  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Getting backbone.js to run a function after constructing a Collection?
  2. var Everything = Backbone.Model.extend({
  3.     url: "/static/data/mydata.json",
  4.     parse: function(data)
  5.     {
  6.         this.set("things", new Things(data.things, {controller: this}));
  7.     }
  8. });
  9.  
  10. var Thing = Backbone.Model.extend({
  11. });
  12.  
  13. var Things = Backbone.Collection.extend({
  14.   model: Thing,
  15.   initialize: function(data, options)
  16.   {
  17.       // HERE I want access to this.models.
  18.       // Unfortunately it has not yet been populated.
  19.       console.log("initialize");
  20.       console.log(this.models);
  21.       // result: []
  22.  
  23.       // And this event never gets triggered either!
  24.       this.on("all", function(eventType)
  25.       {
  26.           console.log("Some kind of event happend!", eventType);
  27.       });
  28.   }
  29. });
  30.  
  31. var everything = new Everything();
  32. everything.fetch();
  33.  
  34. // Some manual poking to prove that the demo code above works:
  35.  
  36. // Run after everything has happened, to prove collection does get created with data
  37. setTimeout(function(){console.log("outside data", everything.get("things").models);}, 1000);
  38. // This has the expected result, prints a load of models.
  39.  
  40.  
  41. // Prove that the event hander works.
  42. setTimeout(function(){console.log("outside trigger", everything.get("things").trigger("change"));}, 1000);
  43. // This triggers the event callback.
  44.        
  45. initialize: function(data, options) {
  46.  
  47.      _.defer(_.bind(this.doSomething, this));
  48. },
  49.  
  50. doSomething: function() {
  51.  
  52.     // now the models are going to be available
  53. }
  54.        
  55. var Everything = Backbone.Model.extend({
  56.     initialize: function() {
  57.         this.things = new Things();
  58.         this.resetThings();
  59.         this.on("change:things", this.resetThings, this);
  60.     },
  61.  
  62.     resetThings: function() {
  63.         // resets the collection with the current data
  64.         this.things.reset(this.get("things"));
  65.     }
  66. });
  67.  
  68. var Thing = Backbone.Model.extend({});
  69.  
  70. var Things = Backbone.Collection.extend({
  71.     model: Thing,
  72.     initialize: function(data, options) {
  73.         this.on("reset", this.initializeThings, this);
  74.     },
  75.  
  76.     initializeThings: function() {
  77.         console.log("initializeThings");
  78.         console.log(this.pluck("id"))
  79.     }
  80. });
  81.  
  82. var everything = new Everything({
  83.     things: []
  84. });
  85. everything.set({
  86.     every: "loaded",
  87.     things: [{"id": "thing1"},{"id": "thing2"}]
  88. });