在制作新模型时将模型 url 与集合 url 相关联
Associating a model url with the collection url while making a new model
我正在尝试制作一个新模型并将其保存在服务器上。我的问题是,当我执行 model.save(obj)
时,它会抛出错误 A "url" property or function must be specified
。
我已经在集合中指定了一个 url,我希望使用它。
我的代码:
class TestModel extends Backbone.Model
initialize: ->
return;
module.exports = TestModel
class TestCollection extends Backbone.Collection
model: TestModel
url: '/models'
parse :(response) ->
return response.data
addModel : (data)->
newModel = new TestModel(data)
newModel.save()
@add(newModel)
module.exports = new TestCollection()
我正在这样调用 addModel 函数
Tests = require 'path/to/test collection'
Tests.addModel(data)
这是一个错误 A "url" property or function must be specified
如果我将我的 addModel 函数修改为以下内容,它就可以工作了! :
addModel : (data)->
newModel = new TestModel(data)
@add(newModel)
newModel.save()
我做错了什么?我想在 save()
之后将模型添加到集合中
您收到 A "url" property or function must be specified
错误,因为它在此 line
上失败
您最后的代码有效,因为添加到集合中的模型引用了 model.collection
,因此在本例中 save
方法可以 resolve url。要开始使用您的初始代码,您应该向您的模型提供 urlRoot
,如下所示:
class TestModel extends Backbone.Model
urlRoot: '/models'
initialize: ->
return;
module.exports = TestModel
Specify a urlRoot if you're using a model outside of a collection, to enable the default url function to generate URLs based on the model id. "[urlRoot]/id"
我正在尝试制作一个新模型并将其保存在服务器上。我的问题是,当我执行 model.save(obj)
时,它会抛出错误 A "url" property or function must be specified
。
我已经在集合中指定了一个 url,我希望使用它。
我的代码:
class TestModel extends Backbone.Model
initialize: ->
return;
module.exports = TestModel
class TestCollection extends Backbone.Collection
model: TestModel
url: '/models'
parse :(response) ->
return response.data
addModel : (data)->
newModel = new TestModel(data)
newModel.save()
@add(newModel)
module.exports = new TestCollection()
我正在这样调用 addModel 函数
Tests = require 'path/to/test collection'
Tests.addModel(data)
这是一个错误 A "url" property or function must be specified
如果我将我的 addModel 函数修改为以下内容,它就可以工作了! :
addModel : (data)->
newModel = new TestModel(data)
@add(newModel)
newModel.save()
我做错了什么?我想在 save()
之后将模型添加到集合中您收到 A "url" property or function must be specified
错误,因为它在此 line
您最后的代码有效,因为添加到集合中的模型引用了 model.collection
,因此在本例中 save
方法可以 resolve url。要开始使用您的初始代码,您应该向您的模型提供 urlRoot
,如下所示:
class TestModel extends Backbone.Model
urlRoot: '/models'
initialize: ->
return;
module.exports = TestModel
Specify a urlRoot if you're using a model outside of a collection, to enable the default url function to generate URLs based on the model id. "[urlRoot]/id"