我正在从Backbone v0.9.2升级到0.9.10,并且在尝试从路由器获取模型时,会收到此错误:
TypeError: model.parse is not a function
if (!model.set(model.parse(resp, options), options)) return false;
它从服务器中取出模型就好了,但随后就爆炸了。
使用:最新的BackboneJS,最新的UnderscoreJS,最新的骨干支持(来自thinkbot),jQuery 1.8.3,Rails后端
以下是相关代码:
App.Models.User = Backbone.Model.extend({
urlRoot: '/users'
});
App.Collections.Users = Backbone.Collection.extend({
model: App.Models.User,
url: '/users'
});
App.Routers.Users = Support.SwappingRouter.extend({
initialize: function() {
this.collection = new App.Collections.Users();
this.el = $('div[role="main"]');
},
routes: {
'!/users/:id' : 'show'
},
show: function(userId) {
var user = new App.Models.User({ id: userId });
user.fetch();
// ...more code
}
});
var App = {
Models: {},
Collections: {},
Views: {},
Routers: {},
initialize: function() {
new App.Routers.Users();
if (!Backbone.history.started) {
Backbone.history.start();
Backbone.history.started = true;
}
};
这与我有Rails(3.2.10)后端的事实有什么关系吗? http://backbonejs.org/#Model-parse
编辑: 这是我的Rails控制器操作
class UsersController < ApplicationController
respond_to :json
def show
@user = find_user params[:id]
respond_to do |format|
format.js { render json: @user }
format.html { redirect_to '/#!/users/' + params[:id] }
end
end