简体   繁体   English

骨干/ Javascript从dom中删除元素

[英]Backbone/Javascript remove element from dom

I have a page with two div classes which is fine. 我有一个包含两个div类的页面,这很好。 One is the collection the other the item. 一个是集合,另一个是项目。

But when i want to select edit, i need to remove the item view and replace it with the edit link, this is not happening and its staying there, below is my edit class would be great. 但是,当我要选择编辑时,我需要删除项目视图并用编辑链接替换它,这没有发生,并且它停留在那里,下面是我的编辑类很棒。

Supernote.Views.Notes ||= {}



class Supernote.Views.Notes.EditView extends Backbone.View

 template : JST["backbone/templates/notes/edit"]



 events :

 "submit #edit-note" : "update"



 update : (e) ->

  e.preventDefault()

  e.stopPropagation()



  @model.save(null,

   success : (note) =>

    @model = note

    window.location.hash = "/#{@model.id}"

  )



  render : ->

   $(@el).html(@template(@model.toJSON() ))



  this.$("form").backboneLink(@model)



  return this

Can you post some more of the code? 您还能发布更多代码吗? When you say 'edit link', do you mean something in the DOM, or a url in the address bar. 当您说“编辑链接”时,是指DOM中的内容,还是地址栏中的URL。

When you call @model.save, Backbone will automatically update the attributes of the model with attributes returned by the server, so the 当您调用@ model.save时,Backbone将使用服务器返回的属性自动更新模型的属性。

@model = note

is not required. 不需要。 But if you are not binding the 'change' event on the model to anything, then the view will not update if the model changes. 但是,如果您没有将模型上的“ change”事件绑定到任何东西,那么如果模型发生更改,则视图将不会更新。

Are you using Backbone routers to handle changes in the location.hash? 您是否正在使用骨干路由器来处理location.hash中的更改?

You can remove a view from the DOM by calling @remove() You could also call $(@el).replace(...) if you wanted to replace it with something. 您可以通过调用@remove()从DOM中删除视图。如果要用某种内容替换它,也可以调用$(@ el).replace(...)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM