在我的index_view上,我想在创建项目时动态添加项目(创建函数在index_view - create_item()中)。我做错了什么。在我的index_view中,我有集合视图,它没有ListenedId,我需要监听(我认为)并在创建新项目时更新我的视图。在index_view初始化我有没有工作的功能,如何让它工作? @listenTo
无效,没有ListenerId,或者我可以定义它?
这是我的索引视图:
//app/assets/javascripts/backbone/views/inventories/index_view.js.coffee
Sprint.Views.Inventories ||= {}
class Sprint.Views.Inventories.IndexView extends Backbone.View
template: JST["backbone/templates/inventories/index"]
initialize: () ->
@options.inventories.bind('reset', @addAll)
@options.inventories.on('add', @addOne) //not working
create_item: (attributes) -> //this is called from my other models view
this.collection = new Sprint.Collections.InventoriesCollection
this.model = new this.collection.model(item: attributes.name, user_id: router.stats.models[0].attributes.user_id)
this.collection.create(this.model.toJSON())
addAll: () =>
@options.inventories.each(@addOne)
addOne: (inventory) =>
view = new Sprint.Views.Inventories.InventoryView({model : inventory})
@$("tbody").append(view.render().el)
render: =>
@$el.html(@template(inventories: @options.inventories.toJSON() ))
@addAll()
return this
这是我的模特:
//app/assets/javascripts/backbone/models/inventory.js.coffee
class Sprint.Models.Inventory extends Backbone.Model
paramRoot: 'inventory'
defaults:
item: null
user_id: ""
class Sprint.Collections.InventoriesCollection extends Backbone.Collection
model: Sprint.Models.Inventory
url: '/inventories'
这是我的路由器:
//app/assets/javascripts/backbone/views/inventories/inventory_view.js.coffee
class Sprint.Routers.DropsRouter extends Backbone.Router
initialize: (options) ->
@inventories = new Sprint.Collections.InventoriesCollection()
@inventories.reset options.inventories
routes:
"index" : "index"
".*" : "index"
send_item_to_inventory: (attributes) ->
@view = new Sprint.Views.Inventories.IndexView(inventories: @inventories)
@view.create_item(attributes)
index: ->
@view = new Sprint.Views.Inventories.IndexView(inventories: @inventories)
$("#inventories").html(@view.render().el)
这是我的广告资源视图:
//app/assets/javascripts/backbone/models/inventory.js.coffee
Sprint.Views.Inventories ||= {}
class Sprint.Views.Inventories.InventoryView extends Backbone.View
template: JST["backbone/templates/inventories/inventory"]
events:
"click .destroy" : "destroy"
initialize: ->
@listenTo @model, 'change', @render
@listenTo @model, 'reset', @render
tagName: "tr"
destroy: () ->
@model.destroy()
this.remove()
return false
window.locition.hash = "#/index"
render: ->
@$el.html(@template(@model.toJSON() ))
return this
答案 0 :(得分:0)
你有这个:
initialize: () ->
@options.inventories.bind('reset', @addAll)
@options.inventories.on('add', @addOne) //not working
表明@options.inventories
是一个集合。然后在同一视图中:
create_item: (attributes) -> //this is called from my other models view
this.collection = new Sprint.Collections.InventoriesCollection
this.model = new this.collection.model(item: attributes.name, user_id: router.stats.models[0].attributes.user_id)
this.collection.create(this.model.toJSON())
您创建了一个新集合,将其分配给@collection
,然后将您的模型添加到@collection
。但是你正在听@options.inventories
。
通常,您会将该集合作为collection
选项传递给视图,而Backbone将自行设置@collection
:
constructor / initialize
new Backbone.Collection([models], [options])
[...]有几个选项,如果提供,会直接附加到收藏集:
model
和comparator
。
所以,如果你说:
new V([...], collection: some_collection)
然后Backbone会在您的视图中将@collection
设置为some_collection
。
如果可以,您还应该使用listenTo
代替on
。
您的观点应该更像这样:
initialize: () ->
@listenTo(@collection, 'reset', @addAll)
@listenTo(@collection, 'add', @addOne)
create_item: (attributes) ->
@collection.create(attributes)
你可以像这样实例化它:
new Sprint.Views.Inventories.IndexView(collection: @inventories)
您必须在其他地方应用类似的更改。
此外,Backbone视图不再为您version 1.1.0 from October 2013设置@options
:
- Backbone Views不再自动附加传递给构造函数的选项
this.options
[...],但如果您愿意,可以自己执行。
因此,您可能需要更新Backbone并修补其他代码。