Backbone.js 如何处理主干js中的归属关系
我有两个模型,用户和图片。用户有很多图片。我想对图片的后端服务器查询进行挂起调用。当前,当我点击暂停按钮时,我得到了用户模型,但我想要图片模型。在我的用户视图中,我有以下代码 用户视图Backbone.js 如何处理主干js中的归属关系,backbone.js,coffeescript,Backbone.js,Coffeescript,我有两个模型,用户和图片。用户有很多图片。我想对图片的后端服务器查询进行挂起调用。当前,当我点击暂停按钮时,我得到了用户模型,但我想要图片模型。在我的用户视图中,我有以下代码 用户视图 class MyApp.Views.User extends Backbone.View initialize: -> @listenTo(@model, 'change', @render) @listenTo(@model, 'destroy', @remove) rend
class MyApp.Views.User extends Backbone.View
initialize: ->
@listenTo(@model, 'change', @render)
@listenTo(@model, 'destroy', @remove)
render: ->
$(@el).html(@template(user: @model))
@fetchPictures()
this
fetchPictures: ->
@picture_collection = new MyApp.Collections.Pictures()
@picture_collection.fetch({
reset: true,
data: { "user_id": @model.get("objectId") }#,
success: (e) ->
for picture in e.models
view = new MyApp.Views.Picture(model: picture)
$("#objects-info").html(view.render().el)
})
class MyApp.Views.Picture extends Backbone.View
template: JST['flagged_objects/picture']
el: 'td'
events: ->
"click #Picure": "deletePicture"
initialize: ->
@model.set('id', this.model.get('objectId'))
@listenTo(@model, 'change', @render)
@listenTo(@model, 'destroy', @remove)
render: ->
$("#object-info").append(@template(entry: @model))
this
deletePicture: (e) ->
e.preventDefault()
console.log @
图片视图
class MyApp.Views.User extends Backbone.View
initialize: ->
@listenTo(@model, 'change', @render)
@listenTo(@model, 'destroy', @remove)
render: ->
$(@el).html(@template(user: @model))
@fetchPictures()
this
fetchPictures: ->
@picture_collection = new MyApp.Collections.Pictures()
@picture_collection.fetch({
reset: true,
data: { "user_id": @model.get("objectId") }#,
success: (e) ->
for picture in e.models
view = new MyApp.Views.Picture(model: picture)
$("#objects-info").html(view.render().el)
})
class MyApp.Views.Picture extends Backbone.View
template: JST['flagged_objects/picture']
el: 'td'
events: ->
"click #Picure": "deletePicture"
initialize: ->
@model.set('id', this.model.get('objectId'))
@listenTo(@model, 'change', @render)
@listenTo(@model, 'destroy', @remove)
render: ->
$("#object-info").append(@template(entry: @model))
this
deletePicture: (e) ->
e.preventDefault()
console.log @
图片集
class MyApp.Collections.Pictures extends Backbone.Collection
model: MyApp.Models.Picture
url: "/api/pictures"
PICUTRE型号
class MyAdmin.Models.Picture extends Backbone.Model
urlRoot: 'api/picture'
idAttribute: 'objectId'
在用户视图中在@model变量中,我得到了用户模型。有没有办法在这里得到图片模型,这样我就可以打电话挂起图片
总之,当我按下suspend按钮时,我只想从集合中暂停图片模型obejct。Suspend本质上是一个更新调用
谢谢,正如我们在评论中所说,deletePicture函数应该位于Picture子视图中,因为您要挂起的是图片模型 我认为你的奇怪行为与你表达观点的方式有关 在用户视图中,应附加图片子视图
fetchPictures: ->
@picture_collection = new MyApp.Collections.Pictures()
@picture_collection.fetch({
reset: true,
data: { "user_id": @model.get("objectId") }#,
success: (e) ->
for picture in e.models
view = new MyApp.Views.Picture(model: picture)
$("#objects-info").append(view.render().el)
})
在子视图的呈现中,您可以在其中访问html函数
render: ->
this.$el.html(@template(entry: @model))
this
让我知道进展如何 你们可以看一看,我相信解决方案比这个简单得多。我只想触发与正在单击的模型相关的操作。为什么不将deletePicture函数移动到图片子视图?我已将deletePicture移动到图片子视图,并定义了el。现在我开始注册click events,但是当我单击最上面的suspend按钮时,会为集合中的所有对象调用deletePicture函数,但单击的对象除外。如果单击第二个挂起按钮,则会为集合中的所有对象调用deletePicture函数,但第一个对象和单击的对象除外,依此类推。我还更新了代码,请再次查看。这只显示最后渲染的图片元素,同时覆盖它之前的所有元素。但单击“挂起”按钮仍会返回集合中的所有元素。“#对象信息”是一个表?”#对象信息”是一个div,而“#对象信息”是一个表。因此,您应该将子视图附加到“#对象信息”中,尽管我不确定问题是否存在。谢谢您这样做了。我会建议大家不要用桌子。