我是一個骨幹新手,所以我有點摸索得到一個應用程序設置。我使用骨幹模板(https://github.com/tbranyen/backbone-boilerplate)和github查看器(https://github.com/tbranyen/github-viewer)作爲參考,但在運行時我似乎正在獲取「this.model未定義」。骨幹樣板:「這個模型是未定義的」
這是我目前的router.js:
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();
this.homepage.fetch();
},
// Shortcut for building a url.
go: function() {
return this.navigate(_.toArray(arguments).join("/"), true);
},
reset: function() {
// Reset collections to initial state.
if (this.homepage.length) {
this.homepage.reset();
}
// Reset active model.
app.active = false;
}
});
return Router;
}
);
而且我homepage.js模塊:提前爲幫助
define([
"app"
],
function(app){
"use strict";
var Homepage = app.module();
Homepage.Model = Backbone.Model.extend({
defaults: function(){
return {
homepage: {}
};
}
});
Homepage.Collection = Backbone.Collection.extend({
model: Homepage.Model,
cache: true,
url: '/app/json/test.json',
initialize: function(models, options){
if (options) {
this.homepage = options.homepage;
}
}
});
Homepage.Views.Index = Backbone.View.extend({
template: "homepage",
el: '#mainContent',
render: function(){
var tmpl = _.template(this.template);
$(this.el).html(tmpl(this.model.toJSON()));
return this;
},
initialize: function(){
this.listenTo(this.options.homepage, {
"reset": function(){
this.render();
},
"fetch": function() {
$(this.el).html("Loading...");
}
});
}
});
return Homepage;
});
謝謝!
更新:多少谷歌搜索(你應該看到我有多少標籤已打開),我覺得我取得進展的一點點,但仍沒有運氣了。我更新了我的路由器具備以下條件:
app.useLayout("main-frame").setViews({
".homepage": new Homepage.Views.Index()
}).render();
我提出了一些修改,我homepage.js模塊到現在這個樣子:
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',
initialize: function(){
var self = this;
this.collection = new Homepage.Collection();
this.collection.fetch().done(function(){
self.render();
});
},
render: function(){
var data = this.collection;
if (typeof(data) === "undefined") {
$(this.el).html("Loading...");
} else {
$(this.el).html(_.template(this.template, data.toJSON()));
}
return this;
}
});
return Homepage;
});
正如你所看到的,我有localStorage的代碼但現在已經發表了評論,因爲我只想讓所有事情都先做好。最終目標是進行初始調用,從JSON文件加載數據,然後使用localStorage繼續。該應用稍後會在用戶與我的應用進行多次交互之後提交數據。
雖然主模塊沒有在主視圖中填充#mainContent容器,但我正在加載主視圖。
我做了所有的搜索引擎優化,我可以但沮喪,它只是沒有爲我沉沒。再次感謝您的關注,並感謝您的反饋!
Oy,謝謝你看這個!我仍然有很多需要完成的工作,原來的模型集合是在我精簡到一個集合的示例代碼中。也就是說,直到不工作,將盡快包括一些代碼... – 2013-02-11 21:08:51
再次感謝您的幫助。我結束了一大堆工作,所以我發佈了一個後續問題:http://stackoverflow.com/questions/14857166/view-loading-before-main-layout-is-loaded – 2013-02-13 15:38:17