这是来自Backbone boilerplate: "this.model is undefined"的后续问题。我能够制定出一些结构,但遇到了一个新问题。我正在使用骨干样板,它使用layoutManager适配器来设置视图。其主要观点,以及随后的第一种观点,似乎是正确加载所有的资产,但在随后的观点似乎前主视图中被加载到被烧成这里是我的路由器文件:查看loading在主版面加载之前
define([
// Application.
"app",
//Modules
"modules/homepage"
],
function (app, Homepage) {
"use strict";
// Defining the application router, you can attach sub routers here.
var Router = Backbone.Router.extend({
initialize: function(){
var collections = {
homepage: new Homepage.Collection()
};
_.extend(this, collections);
app.useLayout("main-frame").setViews({
".homepage": new Homepage.Views.Index(collections)
}).render();
},
routes:{
"":"index"
},
index: function() {
this.reset();
},
// Shortcut for building a url.
go: function() {
return this.navigate(_.toArray(arguments).join("/"), true);
},
reset: function() {
// Reset collections to initial state.
if (this.homepage) {
this.homepage.reset();
}
// Reset active model.
app.active = false;
}
});
return Router;
}
);
这是我的模块:
define([
"app",
["localStorage"]
],
function(app){
"use strict";
var Homepage = app.module();
Homepage.Model = Backbone.Model.extend({
defaults: function(){
return {
homepage: {}
};
}
});
Homepage.Collection = Backbone.Collection.extend({
//localStorage: new Backbone.LocalStorage("Homepage.Collection"),
refreshFromServer: function() {
return Backbone.ajaxSync('read', this).done(function(data){
console.log(data);
//save the data somehow?
});
},
/*model: Homepage.Model,*/
cache: true,
url: '/app/json/test.json',
initialize: function(options){
if (options) {
this.homepage = options.homepage;
}else{
//this.refreshFromServer();
}
}
});
Homepage.Views.Index = Backbone.View.extend({
template: "homepage",
el: '#mainContent',
serialize: function() {
return {
collection: this.options.homepage
};
},
initialize: function(){
this.listenTo(this.options.homepage,{
"reset": this.render,
"fetch": function(){
$(this.el).html("Loading...");
}
});
}
});
return Homepage;
});
正如你所看到的,“主框架”模板是被加载到页面的主模板。我希望之后加载主页模板,并将其填充到ID“#mainContent”中。在这种情况下,“#mainContent”还不存在,所以主页模板不会在任何地方加载。
我遇到了同样的问题与基隆的例子,并有一个关于它的问题。 http://stackoverflow.com/questions/18034235/backbone-localstorage-require-js-uncaught-typeerror-undefined-is-not-a-funct – 2013-08-03 17:46:14