Backbone.js Backbone.xmpp:根据项目id区分项目类型
我想根据带有Backbone.xmpp的item_id来区分节点项。例如,在For each“todo”项中,我希望能够为每个“todo”项分配不同的注释(或多个用户在每个“todo”上发布注释)。我考虑根据todo id将这些注释分配给todo 我可以用关系型主干吗 感谢您的帮助或指导 Edit2:在xmpp服务器的叶节点中存储嵌套模型需要哪些选项Backbone.js Backbone.xmpp:根据项目id区分项目类型,backbone.js,xmpp,Backbone.js,Xmpp,我想根据带有Backbone.xmpp的item_id来区分节点项。例如,在For each“todo”项中,我希望能够为每个“todo”项分配不同的注释(或多个用户在每个“todo”上发布注释)。我考虑根据todo id将这些注释分配给todo 我可以用关系型主干吗 感谢您的帮助或指导 Edit2:在xmpp服务器的叶节点中存储嵌套模型需要哪些选项 TODO和notes是在叶节点上发布的独立项。将注释分配给TODO是否有效?差异将基于项目ID(todoid:todo_1,noteid:todo
$(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;
});
//--------待办事项视图------------------
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;
});
除了让它正常工作之外,没有任何东西特别阻止您将Backbone.relational与Backbone.xmpp一起使用;) 另一方面,Backbone.xmpp提供实时通知,除非再次保存todo模型以便在xmpp节点上重新发布,否则不会触发这些通知。此外,XMPP(以及主干网)支持简单的包含,当您试图围绕它构建关系数据时,您实际上只是在围绕它工作
只提供todo项的注释可能会简单得多,这将节省您与主干网.relational集成的所有工作。谢谢!我真的想把那些笔记发表出来。这只是关于深入了解backbone.xmpp和backbone。稍后,我的应用程序中需要相同的功能。但是仍然不确定是使用主干关系模型还是使用此处建议的嵌套模型:。您是否介意查看我目前在Edit1中编写的代码。它基于最初的todo应用程序,但我会在这个应用程序运行后添加xmpp版本。我在应用程序视图的createOnener()函数上得到“uncaughttypeError:Object函数(obj){returnnewwrapper(obj);}没有方法‘isObject’”。我只想在待办事项上添加注释。这里的Todo模型“有很多”Todo子项(基于Todo模型)。要想弄清楚到底发生了什么有点困难,一把小提琴会有很大帮助!好的,我知道我的主要问题是什么,在编辑2中。不过我明天会创作小提琴。你说的简单包容是什么意思?将注释存储在todo项中的数组中,如{todo1:{body:“todo1”,notes:[{id:“todo1_note1”,body:“note1”}],id:“todo1”}?当我将注释添加到数组“notes”时,backbone.xmpp会发送通知吗?