【问题标题】:backbone rails save not working骨干导轨保存不工作
【发布时间】:2012-12-06 05:58:37
【问题描述】:

我是 Backbone 的新手,在将模型保存到数据库时遇到问题。单击时,“个人资料”字段应更新为“100”,但不起作用:

$->

class User extends Backbone.Model
    url: -> '/users/' + this.get("id")  '.json'

class Users extends Backbone.Collection
    model: User

class UserView extends Backbone.View
    tagName: "li"
    events:
        "click" : "changeProfile"

    render: -> $(@el).html( @model.get "name" )
    changeProfile: -> 
        $('li').removeClass('selected')
        $(@el).addClass('selected')
        @model.set( 'profile' : '100' ).save()

users = new Users
users.url = "/users.json"
users.fetch(
    success: ->
        _.each users.models, (model) ->
            view = new UserView( model: model )
            $('ul').append view.render()
)

“用户”控制器/“用户”模型被搭建(并且控制器以 json 格式呈现)。我正在使用 Backbone 和 Rails 的最新版本。有人可以帮忙吗?

(Chrome中的Javascript控制台注册以下错误:“Uncaught TypeError: number is not a function”)

【问题讨论】:

    标签: ruby-on-rails backbone.js coffeescript


    【解决方案1】:

    Userurl 缺少 +

    class User extends Backbone.Model
        url: -> '/users/' + this.get("id")  '.json'
        #---------------------------------^^
    

    最后一部分是这样解释的:

    this.get("id")('.json')
    

    由于get('id') 返回一个数字,您会收到“数字不是函数”错误。添加缺少的+ 以获得字符串连接或使用CoffeeScript 的插值:

    url: -> '/users/' + this.get('id') + '.json'
    

    url: -> "/users/#{this.get('id')}.json"
    

    【讨论】:

      猜你喜欢
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      相关资源
      最近更新 更多