javascript – Backbone.js – 集合未定义
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – Backbone.js – 集合未定义,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1992字,纯文字阅读大概需要3分钟。
内容图文
![javascript – Backbone.js – 集合未定义](/upload/InfoBanner/zyjiaocheng/748/1fad500bff7a456c83b1f5ab919f5d05.jpg)
我正在尝试学习backbone.js,但我仍然坚持抓取json并添加到集合中.集合未定义,我不知道为什么.
码:
$(document).ready(function() {
(function($) {
//model
window.Wine = Backbone.Model.extend();
window.WineCollection = Backbone.Model.extend({
url: "http://localhost/bootstrap/json.php",
model: Wine
});
//views
window.WineItemView = Backbone.View.extend({
template: _.template($("#wine-item-template").html()),
tagName: 'li',
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
window.WineListView = Backbone.View.extend({
tagName: 'ul',
initialize: function() {
this.model.bind('reset', this.render, this);
},
render: function() {
_.each(this.model.models, function(wine) {
$(this.el).append(new WineItemView({
model: wine
}).render().el);
}, this);
return this;
}
});
window.WineView = Backbone.View.extend({
template: _.template($('#wine-template').html()),
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
//Router
window.AppRouter = Backbone.Router.extend({
routes: {
'': 'home',
'wines/:id': "wineDetails"
},
home: function() {
this.wineList = new WineCollection();
this.wineListV = new WineListView({
model: this.wineList
});
this.wineList.fetch({success: function(){
console.log(this.wineList.models); // => 2 (collection have been populated)
}});
console.log(this.wineListV);
$("#winelist").html(this.wineListV.render().el);
},
wineDetails: function(id) {
this.wine = this.wineList.get(id);
console.log(id);
this.wineView = new WineView({
model: this.wine
});
$("#container").empty().html(this.wineView.render().el);
}
});
})(jQuery);
var app = new AppRouter();
Backbone.history.start();
});
json.php返回:
[
{
"name":"Wine 1",
"price":"100",
"status":"available"
},
{
"name":"Wine 1",
"price":"100",
"status":"available"
},
{
"name":"Wine 1",
"price":"100",
"status":"available"
}
]
我在我的localhost上测试它.
解决方法:
你应该改变
window.WineCollection = Backbone.Model.extend({
model: Wine,
url: "wine.json"
});
至
window.WineCollection = Backbone.Collection.extend({
model: Wine,
url: "wine.json"
});
内容总结
以上是互联网集市为您收集整理的javascript – Backbone.js – 集合未定义全部内容,希望文章能够帮你解决javascript – Backbone.js – 集合未定义所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。