Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 根据单击的图元构造和渲染主干视图_Backbone.js_Coffeescript - Fatal编程技术网

Backbone.js 根据单击的图元构造和渲染主干视图

Backbone.js 根据单击的图元构造和渲染主干视图,backbone.js,coffeescript,Backbone.js,Coffeescript,嗨,我遇到了主干js的问题 我有一个意见 class window.CommentView extends Backbone.View el: $('.comment') initialize: -> @$el.show() events: "click": "renderCommentBoxView" renderCommentBoxView: -> @commentBoxView = new CommentBoxView id

嗨,我遇到了主干js的问题

我有一个意见

class window.CommentView extends Backbone.View
  el: $('.comment')
  initialize: ->
    @$el.show()
  events:
    "click": "renderCommentBoxView"

  renderCommentBoxView: ->
    @commentBoxView = new CommentBoxView
      id: this.$('.comment')['context']['activeElement']['id']
      model: new Item
      el: @el
和评论框视图

 class window.CommentBoxView extends Backbone.View
   el: $('.commentBoxMain')

   events:
     "click.comment": "showCommentBox"
     "click document": "stopEvent"
     "click .login_button": "submitComment"
   initialize: ->
     @showCommentBox()

   stopEvent: (event) ->
     event.stopPropagation()

  showCommentBox: ->
     $(@el).append('<textarea class=\'commentBox\'></textarea><br/><input type=\'button\' class=\'login_button\' name=\'Add a comment\' value=\'Add a comment\'><span class=\'loading\' style=\'display: none;\'>Loading...</span>')
class window.CommentBoxView扩展了主干.View
el:$('commentBoxMain')
活动:
“单击。注释”:“showCommentBox”
“单击文档”:“停止事件”
单击。登录按钮“:“提交建议”
初始化:->
@ShowBox()
stopEvent:(事件)->
event.stopPropagation()
展示框:->
$(@el).append(“
正在加载…”)
现在,用户可以对多个项目发表评论。因此,每当单击comment按钮时,我都会呈现一个名为CommentBoxView的新视图,用于定义元素和模型

问题是我无法获取视图应绑定到的当前单击元素

我的示例HTML如下所示:

   <div id="item_1">
     <a class="comment" href='javascript:void(0)'>Comment</a>
     <div class="commentMainBox"></div>
   </div>
  <div id="item_2">
    <a class="comment" href='javascript:void(0)'>Comment</a>
    <div class="commentMainBox"></div>
  </div>

每当单击注释链接时,我都会构造html并将其转储到commentMainBox中。 但问题是,单击的元素始终是我页面上第一个带有类注释的元素。如何获取当前单击的元素,以便在正确的div中呈现内容

CommentBoxView的元素也应该是
el:$('.commentBoxMain')
,但是如果我分配了这个元素,那么我在视图中不会得到任何渲染,但是当我初始化为
el:$('.comment')
时,我看到了注释框,但是无论单击了哪个注释,它总是在第一条注释下渲染


我哪里出错了?

您不应该将
@el
CommentView
传递到它实例化的
CommentBoxView
;在
CommentBoxView
类中声明
el:$('.commentBoxMain')
也不起作用。正如Backbone.js文档所说:

如果要创建引用DOM中已有元素的视图,请将该元素作为选项传入:
newview({el:existingElement})

否则,
视图将尝试创建新元素。所以你想做的是:

@commentBoxView = new CommentBoxView
  model: new Item
  el: @$('.commentMainBox')

这将找到Comment视图中的
commentMainBox
元素(因为
@$
在其元素范围内),并将其作为
CommentBoxView
的元素提供。

感谢您的解决方案,但它不起作用。此外,commentMainBox不在commentView中。我首先想到在单击注释链接时动态创建commentMainBox视图,但后来我认为如果在注释链接下方定义commentBox视图会更好。我在项目的其他部分也做过类似的事情,他们也做得很好,但这里的情况有点不同。在这里,我需要在多个地方多次渲染相同的视图。我在这里面临的最大问题是,我没有得到当前单击的元素。而且,至少当我单击注释链接时,它应该给我当前元素,而不是给我第一个class='comment'的元素。非常感谢任何帮助:)定义el:$('.commentBoxMain')以引用DOM中的现有元素对我来说很好。虽然在这种情况下,我会使用ID而不是类,因为您的元素是(并且应该是)唯一的。