使用backbone.js更新更改的属性

发布于 2024-12-04 22:59:33 字数 238 浏览 0 评论 0原文

所以我正在设置一个具有更新属性的模型。

然后在我看来,我正在监听该模型的更改事件。

当它触发时,我想我应该使用 model.changedAttributes?我是否向它传递回调?

它应该返回所有已更新或新属性的哈希值?有没有办法知道哪些是更新的,哪些是新的?

一旦我有了更改属性的哈希值,我应该如何进行更新?将对象解析为属性类型,还是应该从一开始就使用更高分辨率的侦听器?

谢谢!

So I'm setting a model with updated attributes.

Then in my view I'm listening for a change event for this model.

When that fires I think I should use model.changedAttributes? Do I pass it a callback?

It should return a hash of all the attributes that are updated, or new? Is there anyway to know which are updated and which are new?

How should I go about updating once I have this hash of changed attributes? Parse the object to type of attribute or should I just use higher resolution listeners from the get go?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下慵懒的猫 2024-12-11 22:59:33

如果您的视图仅显示单个属性 - 例如,如果它是显示模型的某些布尔属性的复选框 - 您应该监听该属性的 'change:attribute_name' 事件,如 Backbone 文档

如果您的视图更复杂并且依赖于多个模型属性 - 例如,如果它是具有“done”、“text”和“dueDate”元素的“To Do”列表项的视图,则侦听“change”事件。在这种情况下,您可以选择更新每个事件的所有元素,也可以使用changedAttributes() 来确定哪些元素需要更新。

为了说明...

使用“change:attribute_name”事件更新属性:

这种样式非常适合简单视图,其中渲染的模型属性的数量<<。 3个左右。不仅如此,代码会变得有点麻烦。

model.bind('change:done', function() {
    view.doneElement.checked = model.get('done');
});
model.bind('change:text', function() {
    view.textElement.value = model.get('text');
});
model.bind('change:dueDate', function() {
    view.dueDateElement.value = model.get('dueDate');
});

更新“更改”事件中的所有内容:

这种样式非常适合渲染 4 个或更多属性的复杂视图(3/4 属性计数只是一个粗略的指导原则,主要基于我个人的意见)。

model.bind('change', function() {
    view.doneElement.checked = model.get('done');
    view.textElement.value = model.get('text');
    view.dueDateElement.value = model.get('dueDate');
});

这样做的缺点是,对于任何更改,视图的每个元素都会更新。因此,例如,如果一个人将待办事项标记为“完成”,则文本将重新呈现,可能会丢失他们在那里可能进行的任何选择。有时这类事情是一个问题,有时则不是 - 你必须根据你的观点到底在做什么来决定。

仅更新自上次“更改”事件以来更改的内容:

这是上述方法的更细微的变化,并且结合了两种方法的优点。它根据changedAttributes()结果更新需要更新的视图元素。

model.bind('change', function() {
  var diff = model.changedAttributes();
  for (var att in diff) {
    switch(att) {
      case 'done':
        view.doneElement.checked = model.get('done');
        break;
      case 'text':
        view.textElement.value = model.get('text');
        break;
      case 'dueDate':
        view.dueDateElement.value = model.get('dueDate');
        break;
    }
  }
});

最后,我要指出的是,还有另一种变体,即让视图存储它所显示的值的哈希值,并将其传递到changedAttributes() 方法中。这通常是没有必要的,所以我不会在这里向您提供无聊的细节。

If your view is only showing a single attribute - for example, if it's a checkbox showing some boolean attribute of your model - you should listen to the 'change:attribute_name' event for that attribute, as described in the Backbone docs.

If your view is more complex and relies on multiple model attributes - for example if it's a view for "To Do" list item that has "done", "text", and "dueDate" elements, then listen for the 'change' event. In this case, you can either either choose to update all of the elements on each event, or you can use changedAttributes() to determine which elements need updating.

To illustrate ...

Update attributes using 'change:attribute_name' events:

This style works well for simple views where the number of model attributes being rendered is < 3 or so. More than that and the code gets a bit cumbersome.

model.bind('change:done', function() {
    view.doneElement.checked = model.get('done');
});
model.bind('change:text', function() {
    view.textElement.value = model.get('text');
});
model.bind('change:dueDate', function() {
    view.dueDateElement.value = model.get('dueDate');
});

Update everything on 'change' events:

This style works well for complex views that render 4 or more attributes (the 3/4 attribute count is just a rough guideline, based mostly on my personal opinion).

model.bind('change', function() {
    view.doneElement.checked = model.get('done');
    view.textElement.value = model.get('text');
    view.dueDateElement.value = model.get('dueDate');
});

The downside to this is that for any change, every element of the view is updated. So, for example, if a person marks a todo item as "done", the text will be re-rendered, possibly losing whatever selection they may have had there. Sometimes that sort of thing is an issue, sometimes it isn't - you'll have to decide based on what exactly your view is doing.

Update only what's changed since the last 'change' event:

This is the more nuanced variation of the above, and combines the best of both approaches. It updates the view elements that need updating based on the changedAttributes() results.

model.bind('change', function() {
  var diff = model.changedAttributes();
  for (var att in diff) {
    switch(att) {
      case 'done':
        view.doneElement.checked = model.get('done');
        break;
      case 'text':
        view.textElement.value = model.get('text');
        break;
      case 'dueDate':
        view.dueDateElement.value = model.get('dueDate');
        break;
    }
  }
});

Finally, I'll note that there's yet another variation on this that involves having the view store a hash of what values it's displaying, and passing that into the changedAttributes() method. That's typically not necessary, so I won't bore you with the details here.

熊抱啵儿 2024-12-11 22:59:33

根据主干文档,更改事件将模型和模型的集合传递给事件处理程序。

该事件在更改发生后触发,因此传递的模型包含更改。

“更改”事件不允许您知道哪些属性已更改或添加。如果您需要根据各个属性执行操作,请使用“change:attribute”事件。

As per the backbone docs the change event passes the model and the model's collection to the event handler.

The event fires AFTER the changes happen, so the passed model contains the changes.

The "change" event doesn't allow you to know which attributes have changed or been added. If you need to make actions based on individual attributes, then use the "change:attribute" events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文