0

Backbone.xmpp で item_id に基づいてノード アイテムを区別したい。たとえば、各「todo」アイテムのtodo.appで、異なるメモを割り当てることができるようにしたい (または複数のユーザーが各「todo」にメモを公開する)。todo id に基づいて、それらのメモを todo に割り当てることを考えました。

Backbone.xmpp でBackboneリレーショナルを使用できますか?

ヘルプやガイドラインをいただければ幸いです。

Edit2: ネストされたモデルを xmpp サーバー上のリーフ ノードに保存するには、どのようなオプションが必要ですか?

  1. todo とメモは、リーフ ノードで公開される個別のアイテムです。Todo にコメントを割り当てると効率的ですか? 区別はアイテム ID (todoid: todo_1、noteid: todo_1_note_1) に基づいて行われます。

  2. todos はアイテムであり、notes は todo アイテム (JSON オブジェクト) 内のオブジェクトの配列ですか? しかし、このソリューションでは、ToDo アイテムの更新になるため、メモが公開されたときに通知を受け取ることはありません。さらに、すべてのメモが 1 つのアイテムに保存されるため、非常に長くなる可能性があります。

  3. もともと私は todo をリーフ ノード (リーフ ノード名または "title" 属性として) にマッピングし、アイテムにメモを付けるという考えを持っていましたが、BB.xmpp はこれまでのところこれをサポートしていないでしょうか?

したがって、私は todo とメモをアイテム ID で区別する最初のソリューションを採用する傾向があります。

Backbone.xmpp でそれを達成するにはどうすればよいですか?

Edit1: コードは、localstorage を使用した元の 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;

// ------------------- Todo ビュー ------------------

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

1 に答える 1

1

Backbone.relational を Backbone.xmpp と一緒に使用することを妨げるものは特にありません;)

一方、Backbone.xmpp は、XMPP ノードで再公開されるように todo モデルを再度保存しない限りトリガーされないリアルタイム通知を提供します。また、XMPP (およびバックボーン) は単純なコンテインメントをサポートしており、その周りにリレーショナル データを構築しようとすると、実際にはそれを回避するだけです。

Todo 項目にメモを提供するだけの方がはるかに簡単な場合があります。これにより、Backbone.relational と統合するためのすべての労力を節約できます。

于 2012-07-03T15:04:40.770 に答える