2012-07-02 28 views
0

我想根据item_id与Backbone.xmpp区分节点项目。例如,在todo.app中,对于每个“待办事项”项目,我希望能够分配不同的笔记(或多个用户在每个“待办事项”上发布笔记)。我想将这些笔记分配给基于待办事项ID的待办事项。Backbone.xmpp:根据项目ID区分项目类型

我可以使用Backbone关系与Backbone.xmpp吗?

任何帮助或指导表示赞赏。

Edit2:我有什么选择将嵌套模型存储在xmpp服务器的叶节点上?

  1. 待办事项和笔记是发布在叶节点上的单独项目。将评论分配给待办事项会有效吗?差异将基于项目id(todoid:todo_1,noteid:todo_1_note_1)。

  2. todos是项目和备注是todo项目(JSON对象)内的对象数组?但有了这个解决方案,我将不会在笔记发布时收到通知,因为它将是待办事项的更新。此外,所有音符都将存储在一个单独的项目中 - 这可能会很长。

  3. 本来我的想法是将叶节点上的待办事项(作为叶节点名称或“标题”属性)和项目上的注释映射到一起,但BB.xmpp目前不支持?

因此,我倾向于第一个解决方案,其中todos和notes由item id区分。

Backbone.xmpp如何实现这一目标?

编辑1:代码是用于本地存储的原始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(); 
} 
}); 

// ------------------ The Application ------------------------

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; 
}); 

回答

1

没有什么特别阻止你使用Backbone.relational和Backbone.xmpp,除了让它工作;

另一方面,Backbone.xmpp提供了不会去的实时通知除非您再次保存待办事项模型,以便将其重新发布到您的XMPP节点上。另外XMPP(以及骨干)支持简单的容器,而当你试图围绕它构建关系数据时,你实际上只是在解决它。

仅仅提供关于待办事项的备注可能会简单得多,这将节省您与Backbone.relational集成的所有工作量。

+0

谢谢!我真的希望这些笔记能够发表。这只是要深入backbone.xmpp和主干。稍后,我需要在我的应用中使用相同的功能。但仍然不确定使用骨干关系或使用嵌套模型建议在这里:http://stackoverflow.com/questions/6353607/backbone-js-structuring-nested-views-and-models。 – genericatz

+0

您是否介意查看我在Edit1中编写的代码。它基于原始的待办事项应用程序,但我会在我得到这个工作后添加XMPP版本。我得到“Uncaught TypeError:Object function(obj){return new wrapper(obj);}在Application View的createOnEnter()函数上没有方法'isObject'”。我只是想在todos上添加注释。这里Todo模型“有许多”todo儿童(基于todo模型)。 – genericatz

+0

搞清楚发生了什么有点难,小提琴会帮助很多! – ggozad