【发布时间】:2017-03-21 06:56:31
【问题描述】:
我正在尝试从本地 json 文件中获取 json 对象。该文件包含一个包含 2 个数据的数组。
我的意见initialize 方法没有给出任何结果。
我在这里犯了什么错误?有人帮帮我吗?
这是我的代码:
var Theater = {
Models: {},
Collections: {},
Views: {},
Templates:{}
}
Theater.Models.Movie = Backbone.Model.extend({});
Theater.Collections.Movies = Backbone.Collection.extend({
model: Theater.Models.Movie,
url: "scripts/data/movies.json",
initialize: function(){
console.log("Movies initialize");
}
});
Theater.Views.Movies = Backbone.View.extend({
initialize: function () {
console.log( 'view', this.collection, this.collection.models.length ) //gives no results
_.bindAll(this, "render");
this.collection.bind("reset", this.render);
},
render: function(){
console.log("render")
console.log(this.collection.length); nothing consoled
}
})
Theater.Router = Backbone.Router.extend({
routes: {
"": "defaultRoute"
},
defaultRoute: function () {
console.log("defaultRoute");
Theater.movies = new Theater.Collections.Movies()
new Theater.Views.Movies({ collection: Theater.movies });
Theater.movies.fetch().done(function(){
console.log(Theater.movies.length) //2
});
}
})
var appRouter = new Theater.Router();
Backbone.history.start();
【问题讨论】:
标签: json backbone.js backbone-views