Chr*_*ois 12 javascript backbone.js backbone-relational
我想使用backbone-relational在我的backbone.js应用程序中使用嵌套模型.
我已经能够按照文档中的示例来创建嵌套对象(例如,一对多关系).但是我不明白如何以更新上层对象的方式绑定较低级别的元素.我认为一个有用的应用程序将是一个非常有用的教程.
所以我的问题是:如何使用以下方式扩展Todos教程backbone-relational:
更新:我为这个问题创建了一个jsfiddle.到目前为止,我有:
TodoSubitem模型和一个TodoSubitemList集合Todo模型以扩展RelationalModel而不是Model与之HasMany相关TodoSubitemsubitem-template在html代码中添加了一个但我仍然不确定如何:
subitems仅在单击Tododiv 时显示Todo对象的属性,但仍然TodoSubitemView将DOM元素绑定到它们(例如<li>标签).Pau*_*aul 11
在这种情况下,我不认为我会创建一个单独的'TodoSubItem' - 为什么不HasMany从Todo-> Todo 创建一个关系,所以Todo可以有0 ..*children和0..1 parent?
这样,您可以重新使用订单逻辑(如果您将其更改为每个集合应用),可以根据需要创建更深的嵌套级别(或者如果您还需要将其限制到某个深度),等等.事情需要更新,以适应这一点 - 例如,保留一个子视图列表,以便您可以循环它们以标记每个完成,并维护(和更新)订单每个TodoList.
无论如何,大概是一个可能的解决方案,让你开始,作为你当前版本的一种差异(对不起,它完全未经测试,因此可能包含可怕的错误):
//Our basic **Todo** model has `text`, `order`, and `done` attributes.
window.Todo = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'children',
relatedModel: 'Todo',
collectionType: 'TodoList',
reverseRelation: {
key: 'parent',
includeInJSON: 'id'
}
}],
initialize: function() {
if ( !this.get('order') && this.get( 'parent' ) ) {
this.set( { order: this.get( 'parent' ).nextChildIndex() } );
}
},
// Default attributes for a todo item.
defaults: function() {
return { done: false };
},
// Toggle the `done` state of this todo item.
toggle: function() {
this.save({done: !this.get("done")});
}
nextChildIndex: function() {
var children = this.get( 'children' );
return children && children.length || 0;
}
});
// The DOM element for a todo item...
window.TodoView = Backbone.View.extend({
//... is a list tag.
tagName: "li",
// Cache the template function for a single item.
template: _.template($('#item-template').html()),
// The DOM events specific to an item.
events: {
'click': 'toggleChildren',
'keypress input.add-child': 'addChild',
"click .check" : "toggleDone",
"dblclick div.todo-text" : "edit",
"click span.todo-destroy" : "clear",
"keypress .todo-input" : "updateOnEnter"
},
// The TodoView listens for changes to its model, re-rendering.
initialize: function() {
this.model.bind('change', this.render, this);
this.model.bind('destroy', this.remove, this);
this.model.bind( 'update:children', this.renderChild );
this.model.bind( 'add:children', this.renderChild );
this.el = $( this.el );
this.childViews = {};
},
// Re-render the contents of the todo item.
render: function() {
this.el.html(this.template(this.model.toJSON()));
this.setText();
// Might want to add this to the template of course
this.el.append( '<ul>', { 'class': 'children' } ).append( '<input>', { type: 'text', 'class': 'add-child' } );
_.each( this.get( 'children' ), function( child ) {
this.renderChild( child );
}, this );
return this;
},
addChild: function( text) {
if ( e.keyCode == 13 ) {
var text = this.el.find( 'input.add-child' ).text();
var child = new Todo( { parent: this.model, text: text } );
}
},
renderChild: function( model ) {
var childView = new TodoView( { model: model } );
this.childViews[ model.cid ] = childView;
this.el.find( 'ul.children' ).append( childView.render() );
},
toggleChildren: function() {
$(this.el).find( 'ul.children' ).toggle();
},
// Toggle the `"done"` state of the model.
toggleDone: function() {
this.model.toggle();
_.each( this.childViews, function( child ) {
child.model.toggle();
});
},
clear: function() {
this.model.set( { parent: null } );
this.model.destroy();
}
// And so on...
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10670 次 |
| 最近记录: |