我正在尝试学习 Backbone,但似乎无法弄清楚我在这里做错了什么。我的App.wishlists.create(data);
线路正在返回Uncaught TypeError: object is not a function
。任何帮助将非常感激!
/* -------------------------------------------------------------------------- */
/* src/models/wishlist.js
/* -------------------------------------------------------------------------- */
(function($, App, Wishlist) {
Wishlist.Model = Backbone.Model.extend({
defaults: {
title: ''
}
});
App.wishlist = new Wishlist.Model;
}(jQuery, WL.module('app'), WL.module('wishlist')));
/* -------------------------------------------------------------------------- */
/* src/collections/wishlists.js
/* -------------------------------------------------------------------------- */
(function($, App, Wishlist) {
Wishlist.Collection = Backbone.Collection.extend({
model: App.wishlist,
localStorage: new Backbone.LocalStorage('wishlists-backbone')
});
App.wishlists = new Wishlist.Collection;
}(jQuery, WL.module('app'), WL.module('wishlist')));
/* -------------------------------------------------------------------------- */
/* src/views/wishlist/edit.js
/* -------------------------------------------------------------------------- */
(function($, App, Wishlist) {
Wishlist.Views.Edit = Backbone.View.extend({
el: '#main',
events: {
'submit form': 'save'
},
template: _.template($('#wishlist-edit-template').html()),
render: function() {
this.$el.html(this.template());
},
save: function(e) {
e.preventDefault();
var data = $(e.target).serializeObject();
App.wishlists.create(data);
//e.preventDefault();
}
});
App.wishlistEditView = new Wishlist.Views.Edit;
}(jQuery, WL.module('app'), WL.module('wishlist')));
/* -------------------------------------------------------------------------- */
/* src/app.js
/* -------------------------------------------------------------------------- */
var WL = WL || {};
WL.module = _.memoize(function(module) {
return { Views: {} };
});
(function($, App) {
var Router = Backbone.Router.extend({
routes: {
'': 'home',
'new': 'edit',
'edit/:id': 'edit'
}
});
// on DOM ready
$(function() {
App.router = new Router;
App.router.home = function(params) {
new App.Views.Main;
};
App.router.edit = function(params) {
App.wishlistEditView.render();
};
App.router.on('route', function(route, params) {
App.router[route](params);
});
Backbone.history.start({ pushState: true });
});
}(jQuery, WL.module('app')));