[英]Backbone console.log attributes and pass them to the View
I am not sure if I am using Models and Collections correctly. 我不确定我是否正确使用模型和集合。 If I'm not I would really appreciate any guidance or advice into what I am doing wrong.
如果我不是,那么我将非常感谢任何关于我做错事情的指导或建议。
I have set up a Model and a Collection. 我已经建立了一个模型和一个集合。 The Collection has a url which is executed using the
.fetch()
method. 集合具有使用
.fetch()
方法执行的url。 I pass the Collection to the View where I log the results to the console. 我将集合传递给视图,然后将结果记录到控制台。 When I
console.log(this.model)
in the View I see the attributes nested a few levels deep. 当我在视图中
console.log(this.model)
时,我看到属性嵌套了console.log(this.model)
。 I would like to see the attributes in the console.log. 我想在console.log中看到属性。 The .toJSON() method doe not seem to work.
.toJSON()方法似乎不起作用。
Here's a Fiddle to my current code: http://jsfiddle.net/Gacgc/ 这是我当前代码的小提琴: http : //jsfiddle.net/Gacgc/
Here is the JS: 这是JS:
(function () {
var DimensionsModel = Backbone.Model.extend();
var setHeader = function (xhr) {
xhr.setRequestHeader('JsonStub-User-Key', '0bb5822a-58f7-41cc-b8a7-17b4a30cd9d7');
xhr.setRequestHeader('JsonStub-Project-Key', '9e508c89-b7ac-400d-b414-b7d0dd35a42a');
};
var DimensionsCollection = Backbone.Collection.extend({
model: DimensionsModel,
url: 'http://jsonstub.com/calltestdata'
});
var dimensionsCollection = new DimensionsCollection();
var DimensionsView = Backbone.View.extend({
el: '.js-container',
initialize: function (options) {
this.model.fetch({beforeSend: setHeader});
_.bindAll(this, 'render');
this.model.bind('reset', this.render());
return this;
},
template: _.template( $('#dimensions-template').html() ),
render: function () {
console.log( this.model.toJSON() ); //Why does this return an empty array???
return this;
}
});
var myView = new DimensionsView({model: dimensionsCollection});
}());
Is this what you're looking for? 这是您要找的东西吗?
If you're passing a collection
to the view you should assign it to the collection
property: 如果要将
collection
传递给视图,则应将其分配给collection
属性:
// It's a collection. Backbone views have a collection
// property. We should totally use that!
var myView = new DimensionsView({collection: dimensionsCollection});
When you attempt to bind the reset
event to your view's render
function, you're actually invoking the function immediately (by including the braces): 当您尝试将
reset
事件绑定到视图的render
函数时,实际上是在立即调用该函数(包括花括号):
// Omit the braces to assign the function definition rather than invoke
// it directly (and immediately)
this.model.bind('reset', this.render);
But that's beside the point, because backbone's collection
doesn't trigger a reset
event (see documentation ). 但这很重要,因为主干的
collection
不会触发reset
事件(请参阅文档 )。 One approach would be to assign the view's render function to the success parameter of the options object you pass to your collection: 一种方法是将视图的render函数分配给传递给集合的options对象的success参数:
var self = this;
this.collection.fetch({
beforeSend: setHeader,
success: function() {
self.render();
}
});
Finally, you need a parse
function in your collection to pull the dimensions array out of the JSON you're loading: 最后,您需要在集合中使用
parse
函数将维度数组从要加载的JSON中拉出:
var DimensionsCollection = Backbone.Collection.extend({
model: DimensionsModel,
url: 'http://jsonstub.com/calltestdata',
parse: function(response) {
return response.dimensions;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.