2011-10-17 92 views
3

我有一个Backbone模型line,其中包含模型Stop的集合。 在某一点上,我想遍历线路中的停靠点,并使用下划线功能reduce获得沿线的总行程时间。Backbone集合中消失的模型

但这并不奏效。似乎某些时候收藏会发生某些事情。 它似乎只包含一个没有有意义属性的对象,尽管我知道它已经用四个有效属性的停止模型填充。

型号:

App.models.Line = Backbone.Model.extend({ 
    initialize: function() { 
     var stops = new App.models.Stops({ 
      model: App.models.Stop, 
      id: this.get("id") 
     }); 
     stops.fetch(); 
     this.set({ 
      stops: stops 
     }); 
     this.set({unique: true}); 
     this.calculateTotalTime(); 
    }, 
    calculateTotalTime: function() { 
     this.get("stops").each(function(num) { 
      console.log("Model!"); 
     }); 
     console.log("Unique: ", this.get("unique")); 
    } 
}); 

控制台输出的方法是:

Model! 
Unique: true 

应该有四 “的模型!”,因为模型的数量为四。

最奇怪的是,一切都工作得很好,在控制台:

window.line.get("stops").each(function(num) { 
      console.log("Model!"); 
     }); 
Model! 
Model! 
Model! 
Model! 

的JS与链轮编译:

//= require ./init 

//= require ./lib/jquery 
//= require ./lib/underscore 
//= require ./lib/backbone 
//= require ./lib/raphael 

//= require_tree ./controllers 
//= require_tree ./models 
//= require_tree ./views 

//= require ./main 

init.js:

window.App = {}; 
App.views = []; 
App.models = []; 

主.js:

$(function() { 
    window.line = new App.models.Line({name: "4", id: 4}); 
    window.lineView = new App.views.Line({model: line}); 
    $("#outer").append(lineView.render().el); 
}); 

一些其它奇怪的行为:

console.log(this.get("stops"))在模型产生此相当正常对象:

child 
    _byCid: Object 
    _byId: Object 
    _onModelEvent: function() { [native code] } 
    _removeReference: function() { [native code] } 
    id: 4 
    length: 4 
    models: Array[4] 
    0: Backbone.Model 
    1: Backbone.Model 
    2: Backbone.Model 
    3: Backbone.Model 
    length: 4 
    __proto__: Array[0] 
    __proto__: ctor 

但主叫console.log(this.get("stops").models),这应该产生阵列,仅此返回,单个对象的阵列没有有用的属性:

[ 
    Backbone.Model 
    _callbacks: Object 
    _changed: false 
    _changing: false 
    _escapedAttributes: Object 
    _previousAttributes: Object 
    attributes: Object 
    id: 4 
    model: function(){ return parent.apply(this, arguments); } 
    __proto__: Object 
    cid: "c1" 
    id: 4 
    __proto__: Object 
] 

我怀疑这是所有下降到约的性质有些误解。很高兴为您提供任何帮助。

回答

6

stops.fetch()是一个异步进程,所以在服务器返回提取结果之前,您写入的代码很可能会被触发。

您需要修改代码以在获取回来后运行所有内容。要做到这一点最简单的方法是从stops收集reset事件:

App.models.Line = Backbone.Model.extend({ 
    initialize: function() { 
     var stops = new App.models.Stops({ 
      model: App.models.Stop, 
      id: this.get("id") 
     }); 


     // store the stops, and then bind to the event 
     this.set({stops: stops}); 
     stops.bind("reset", this.stopsLoaded, this); 
     stops.fetch(); 

    }, 

    stopsLoaded: function(){ 
     // get the stops, now that the collection has been populated 
     var stops = this.get("stops"); 
     this.set({unique: true}); 
     this.calculateTotalTime(); 
    }, 

    calculateTotalTime: function() { 
     this.get("stops").each(function(num) { 
      console.log("Model!"); 
     }); 
     console.log("Unique: ", this.get("unique")); 
    } 
}); 

它工作在控制台的原因是因为你输入了代码,以评估模型的停止时,fetch调用有已经返回并填充了该集合。

希望可以帮到

+0

是的,这解决了它。这就是为什么我爱Stackoverflow! :) – Jesper

+0

为了清楚起见,你应该添加一个'停止。fetch()'到initialize-method的末尾。现在,没有什么是触发'重置'事件 – Jesper

+0

@jesper - OOPS!对于那个很抱歉。 :)我编辑了包含该调用的答案。 –

相关问题