Javascript 将集合视图绑定到另一个视图
我将主干和主干rails gem一起使用,它有自己的模板和项目结构 问题是,它在一个div上放置了4个不同的视图,所以我所做的是另一个div,现在模型、显示、编辑视图被分配给另一个视图,基本上我可以在页面左侧和中间的所有东西都有一个列表。 问题是我现在无法重定向,因此当我更新或创建新的“注释”时,列表视图不会刷新 列表视图:Javascript 将集合视图绑定到另一个视图,javascript,ruby-on-rails,backbone.js,coffeescript,Javascript,Ruby On Rails,Backbone.js,Coffeescript,我将主干和主干rails gem一起使用,它有自己的模板和项目结构 问题是,它在一个div上放置了4个不同的视图,所以我所做的是另一个div,现在模型、显示、编辑视图被分配给另一个视图,基本上我可以在页面左侧和中间的所有东西都有一个列表。 问题是我现在无法重定向,因此当我更新或创建新的“注释”时,列表视图不会刷新 列表视图: Supernote.Views.Notes ||= {} class Supernote.Views.Notes.IndexView extends Back
Supernote.Views.Notes ||= {}
class Supernote.Views.Notes.IndexView extends Backbone.View
template: JST["backbone/templates/notes/index"]
initialize: () ->
@options.notes.bind('reset','change', @addAll)
addAll: () =>
@options.notes.each(@addOne)
addOne: (note) =>
view = new Supernote.Views.Notes.NoteView({model : note, collection: @options.notes})
@$("li").append(view.render().el)
render: =>
$(@el).html(@template(notes: @options.notes.toJSON() ))
@addAll()
return this
编辑视图:
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
事件是你需要的,
将模型添加到集合时(新注释)
它在集合本身上引发add
事件
因此,在你的收藏中,你可以抓住它并用它做些事情
var myCollection = Backbone.Collection.extend({
//... whole lot of irrelevant stuff goes here :)
});
var myCollectionListView = Backbone.View.extend({
initialize: function(){
_.bindAll(this, 'onAdd');
this.collection.bind('add', this.onAdd);
}
onAdd: function(m){
// either re-render the whole collection
this.render();
// or do something with the single model
var item = $('<li>' + m.get('property') + '</li>');
$('#listview').append(item);
}
});
var myItems = new myCollection({});
var listview = new myCollectionListView({ collection: myItems });
谢谢,但渲染调用不是仅用于视图吗?或者你是说我在列表视图中实例化了collection对象?哦,很抱歉,你是对的,当然是在collection视图中,你可以绑定到collection.add事件和这个.collection.bind()…太棒了,谢谢你,工作很愉快,所以你说我现在需要绑定其他操作,对吗?你可以绑定任何/每个集合事件是的,添加仅为1,你可以执行删除、重置,在此处查找所有可能事件的更多信息:谢谢你的帮助,你太棒了。你知道我现在怎么做认证吗?
initialize: function() {
this.bind('change', this.modelChanged);
},
modelChanged: function(m){
// do something, re-render the view, or anything else...
}