2013-03-12 74 views
0

我開始與骨幹和Laravel,我有幾個問題,因爲我沒有找到任何西班牙語(也許我不知道如何搜索,因此它更容易問)。查看不顯示模板中的骨幹模型

這裏是我的模型:

window.mMateria = Backbone.Model.extend({ 
    defaults: { 
     nombremateria: "" 
    }, 
}); 

window.cMaterias = Backbone.Collection.extend({ 
    url: "materias", 
    model: mMateria, 
    initialize: function() { 
     this.fetch(); 
    } 
}); 

這裏是我的看法:

window.vMaterias = Backbone.View.extend({ 
    tagName: 'ul', 
    model: cMaterias, 
    className:'list-materias', 

    initialize: function() { 
     _.bindAll(this, "render"); 
    }, 
    render: function(){ 
     $(this.el).append("Renderizando!"); //It appears 
     _.each(this.model.models, function (aMater) { 
      console.log(aMater); //HERE IT DOESN'T ENTER, doesn't show anything 
      $(this.el).append(new vMateria({model:aMater}).render().el); 
     }, this); 
     return this; 
    }, 
    el: $(".container-fluid") 
}); 

window.vMateria = Backbone.View.extend({ 
    initialize:function() { 
     _.bindAll(this, "render"); 
     this.model.bind("change", this.render(), this); 
    }, 
    render: function() { 
     this.$el.html(this.template(this.model.toJSON())); 
     return this; 
    }, 
    className: "item-materia", 
    el: $(".container-fluid"), 

    template: _.template($('#pl_materia').val()), 
}); 

然後初始化:

cmaterias = new cMaterias(); 
    console.log(cmaterias); //it returns 41 signatures 
    vmaterias = new vMaterias({model: cmaterias}); 
    console.log(vmaterias); //Shows child {cid: "view1", model: child, ... 
    vmaterias.render().el; 

請幫助我,請原諒我的英語,我不知道Laravel與 return Response::eloquent(Materia::all());是問題所在。儘可能具有特定性。 Dios los bendiga。

回答

0

嘗試這些更改。

window.vMaterias = Backbone.View.extend({ 
    tagName: 'ul', 
    className:'list-materias', 
    render: function(){ 
     this.$el.empty(); 
     this.$el.append("Renderizando!"); //It appears 
     this.collection.each(function (aMater) { 
      console.log(aMater); //HERE IT DOESN'T ENTER, doesn't show anything 
      this.$el.append(new vMateria({model:aMater}).render().el); 
     }, this); 
     return this; 
    }, 
    el: $(".container-fluid") 
}); 

cmaterias = new cMaterias(); 
console.log(cmaterias); //it returns 41 signatures 
vmaterias = new vMaterias({collection: cmaterias}); 
+0

HOLA,它顯示在控制檯: 孩子{車型:數組[0],長度:0,_byId:對象,構造:函數,網址: 「materias」 ...} _byId:對象 _idAttr:「 ID「 長度:41 模型:數組[41] ---但仍然顯示在瀏覽器中沒什麼。 – bluesky777 2013-03-13 00:48:25

+0

$(「。container-fluid」)實際上在DOM中嗎?你看到「Renderizando」了嗎?哦,你幾乎肯定不想從'.val()'得到你的模板。試試'template:_.template($('#pl_materia')。html())',假設'#pl_materia'是一個腳本標記,對不對? – 2013-03-13 01:55:50

+0

是的,我看到「Renderizado」,我的pl_materia是一個textarea,所以它是正確的,但'console.log(vmaterias);'顯示:「child {models:Array [0],...」意思是這個集合沒有被正確填充,但它裏面有41個對象,我該怎麼做? – bluesky777 2013-03-13 11:54:23