2013-10-15 162 views
0

尽管阅读了有关此问题的各种解决方案并试用了这些方法,但我仍在努力访问我的集合中的模型 - 这显然是我没有收到的东西 - 请帮助!在骨干集合中访问模型

我有其中的数据来自一个XML文件的集合中,如下所示:

define([ 
    'underscore', 
    'backbone', 
    'localstorage', 
    'models/script/ScriptModel' 
], function(_, Backbone, LocalStorage, ScriptModel) { 

    var ScriptsCollection = Backbone.Collection.extend({ 

     model : ScriptModel, 

     url: '/data/script.xml', 

     parse: function (data) { 
     var parsed = []; 
      $(data).find('play').each(function (index) { 
       var title = $(this).find('title').text(); 
       var author = $(this).find('author').text(); 
       parsed.push({ 
        title: title, 
        author: author 
       }); 
      }); 

      return parsed; 
     }, 

     fetch: function (options) { 
      options = options || {}; 
      options.dataType = "xml"; 

      return Backbone.Collection.prototype.fetch.call(this, options); 
     } 
    }); 

    return ScriptsCollection; 
}); 

这一切都似乎是工作好了,那么我认为我做到以下几点:

define([ 
    'jquery', 
    'underscore', 
    'backbone', 
    'views/settings/SettingsView', 
    'models/script/ScriptModel', 
    'collections/scripts/ScriptsCollection',  
    'text!templates/script/scriptTemplate.html' 
],  
    function($, _, Backbone, SettingsView, ScriptModel, ScriptsCollection, scriptTemplate) { 

    var ScriptView = Backbone.View.extend({ 
     el : $("#container"), 

     initialize: function(){ 
      this.collection = new ScriptsCollection();  
      this.collection.fetch(); 
     }, 

     render : function() {    
      this.$el.html(scriptTemplate); 

      //add the settings view 
      var settingsView = new SettingsView(); 
      settingsView.render();    
     } 
    }); 

    return ScriptView; 
}); 

现在,如果我控制台登录“this.collection”我可以看到:

child {length: 0, models: Array[0], _byId: Object, constructor: function, model: function…} 
    _byId: Object 
    length: 1 
    models: Array[1] 
    0: child 
    _changing: false 
    _events: Object 
    _pending: false 
    _previousAttributes: Object 
    attributes: Object 
    author: "Eric and Ernie" 
    title: "The Play Wot I Wrote" 
    __proto__: Object 
    changed: Object 
    cid: "c3" 
    collection: child 
    __proto__: Surrogate 
    length: 1 
    __proto__: Array[0] 
    __proto__: Surrogate 

凡在“模型”我可以看到我的数据'属性',但如果我登录'this.collection.models',我看到一个空数组。我也可以看到,模型的长度是0,所以我很困惑这个?

我只是似乎无法弄清楚为什么这是这种情况,虽然我的猜测是我只是缺少一些基本的东西? - 骨干noob我很害怕!

任何想法........任何人?

在此先感谢

回答

0

你可以在你的代码试试这个: VAR ScriptView = Backbone.View.extend({ EL:$( “#集装箱”),

initialize: function(){ 
     this.collection = new ScriptsCollection();  
     this.collection.fetch({reset: true}); 
     var that = this; 
     this.collection.bind('reset', that.render, that.collection); 
    }, 

    render : function() {    
     this.$el.html(scriptTemplate); 
     console.log(this.collection); 
     //add the settings view 
     var settingsView = new SettingsView(); 
     settingsView.render();    
    } 
}); 

现在检查在渲染方法中收集

+0

在骨干网1.0中改变抓取处理程序请参阅链接:http://backbonejs.org/#changelog – SR5

+0

嗨SR5&谢谢,不幸的是,当我按照建议登录渲染方法时,获得了相同的结果。现在也出现错误o f'** Uncaught TypeError:无法调用由'** this.collection.bind('reset',this.render); **'引起的未定义**'的方法'html'。我想我通过在旧版本的Backbone中读取重置的地方 - 我正在使用1.1.0 - 这可能是我想知道的问题的一部分吗? – dooweb

+0

更新了答案。请检查。做了相同的更改,并在渲染方法中获得了集合。 – SR5