我使用require.js用于使用骨干的模块化appraoch,问题是收集。 Fetch不是获取记录,我正在传递成功方法,但它永远不会被调用。我确定url被击中,因为我能够在mozilla控制台中看到它。以下是我的代码:
收藏:
define(['jquery', 'underscore', 'backbone', 'models/news', 'urls'], function($, _, Backbone, News, Urls) {
var newsCollection = Backbone.Collection.extend({
model : News,
url : Urls.buildUrl().getNewsUrl(),
initialize : function() {
// alert(this.url);
},
parse : function(response) {
alert(response);
}
});
return newsCollection;
});
路由器
define(['jquery', 'underscore', 'backbone', 'models/news', 'collections/news', 'views/newsList', 'urls', 'jqm'], function($, _, Backbone, newsModel, newsCollection, newsListView, Urls) {'use strict';
var newsRouter = Backbone.Router.extend({
routes : {
"" : 'newsList'
},
newsList : function() {
this.newsColl = new newsCollection();
this.newsColl.fetch({success: function() {
alert('success');
}});
var showNews = new newsListView({
model : this.newsColl
});
showNews.render();
this.changePage(showNews);
},
changePage : function(view) {
//add the attribute 'data-role="page" ' for each view's div
view.$el.attr('data-role', 'page');
//append to dom
$('body').append(view.$el);
if (!this.init) {
$.mobile.changePage($(view.el), {
changeHash : false
});
} else {
this.init = false;
}
}
});
return newsRouter;
});