我想根据 item_id 和 Backbone.xmpp 来区分节点项目。例如,在每个“待办事项”项目的todo.app中,我希望能够分配不同的注释(或多个用户在每个“待办事项”上发布注释)。我想根据 todo id 将这些笔记分配给 todo。
我可以将 Backbone 关系与Backbone.xmpp一起使用吗?
任何帮助或指导表示赞赏。
Edit2:我必须将嵌套模型存储在 xmpp 服务器上的叶节点中吗?
todos 和 notes 是发布在叶节点上的单独项目。将评论分配给待办事项是否有效?区分将基于项目 ID(todoid:todo_1,noteid:todo_1_note_1)。
todos 是项目,notes 是 todo 项目中的对象数组(JSON 对象)?但是使用此解决方案时,我不会在发布笔记时收到通知,因为它将是待办事项的更新。此外,所有笔记都将存储在一个项目中 - 这可能会很长。
最初我的想法是在叶节点上映射待办事项(作为叶节点名称或“标题”属性)和项目注释,但到目前为止 BB.xmpp 不支持这一点,对吧?
因此,我倾向于第一个解决方案,其中 todos 和 notes 由 item id 区分。
Backbone.xmpp 中如何实现这一点?
Edit1:该代码适用于带有本地存储的原始 todo.app。
$(function(){
// ------------------- Todo Model ------------------
var Todo = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: "children",
relatedModel: "Todo",
collectionType: "TodoList",
reverseRelation: {
key: "parent",
includeInJSON: "id"
}
}],
initialize: function() {
console.log("MODEL: initialize()");
if (!this.get("order") && this.get ("parent")) {
this.set( {order: this.get("parent").nextChildIndex() });
}
},
defaults: function() {
console.log("MODEL: defaults()");
return {
done: false,
content: "default content" };
},
nextChildIndex: function() {
var children = this.get( 'children' );
return children && children.length || 0;
},
clear: function() {
this.destroy();
}
});
// -------------------- 待办事项集合 ------------------
var TodoList = Backbone.Collection.extend({
model: Todo,
// Save all of the todo items under the `"todos"` namespace.
localStorage: new Store("todos-backbone"),
done: function() {
return this.filter(function(todo){ return todo.get('done'); });
},
});
var Todos = new TodoList;
// ------------------- 待办事项视图 ------------------
var TodoView = Backbone.View.extend({
tagName: "li",
template: _.template($('#item-template').html()),
events: {
"keypress input.add-child": "addChild",
"click .check" : "toggleDone",
"dblclick label.todo-content" : "edit",
"click span.todo-destroy" : "clear",
"keypress .todo-input" : "updateOnEnter",
"blur .todo-input" : "close"
},
initialize: function() {
console.log("TODOVIEW: initialize()");
this.model.bind('change', this.render);
this.model.bind('destroy', this.remove);
this.model.bind("update:children", this.renderChild);
this.model.bind("add:children", this.renderChild);
this.el = $( this.el );
this.childViews = {};
},
render: function() {
console.log("TODOVIEW: render()");
$(this.el).html(this.template(this.model.toJSON()));
this.setText();
this.input = this.$('.todo-input');
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) {
console.log("TODOVIEW: addChild()");
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){
console.log("TODOVIEW: renderChild()");
var childView = new TodoView({ model: model});
this.childViews[model.cid] = childView;
this.el.find("ul.children").append(childView.render());
},
// Remove the item, destroy the model.
clear: function() {
console.log("TODOVIEW: clear()");
this.model.set({parent: null});
this.model.destroy();
//this.model.clear();
}
});
// - - - - - - - - - 应用程序 - - - - - - - - - - - -
var AppView = Backbone.View.extend({
el: $("#todoapp"),
statsTemplate: _.template($('#stats-template').html()),
events: {
"keypress #new-todo": "createOnEnter",
"keyup #new-todo": "showTooltip",
"click .todo-clear a": "clearCompleted",
"click .mark-all-done": "toggleAllComplete"
},
initialize: function() {
console.log("APPVIEW: initialize()");
_.bindAll(this, 'addOne', 'addAll', 'render', 'toggleAllComplete');
this.input = this.$("#new-todo");
Todos.bind('add', this.addOne);
Todos.bind('reset', this.addAll);
Todos.bind('all', this.render);
Todos.fetch();
},
render: function() {
},
addOne: function(todo) {
var view = new TodoView({model: todo});
this.$("#todo-list").append(view.render().el);
},
addAll: function() {
Todos.each(this.addOne);
},
// Generate the attributes for a new Todo item.
newAttributes: function() {
return {
content: this.input.val(),
order: Todos.nextOrder(),
done: false
};
},
createOnEnter: function(e) {
console.log("APPVIEW: createOnEnter()");
if (e.keyCode != 13) return;
Todos.create( this.newAttributes());
this.input.val('');
},
});
var App = new AppView;
});