使用 Backbone.js 和 jquery.datalink.js
是否可以同时使用 Backbone.js 和 jquery.datalink.js 将 Backbone 模型链接到一组字段,如果可以,如何链接?
例如,给定:
<div id="person">
<label for="name">Name:</label>
<input type="text" name="name" id="name" />
</id>
并且
var m = Backbone.Model.extend({});
似乎可以这样说(但不起作用):
$("#person").link(m);
通过“不起作用”,我的意思是当我更改模型时,链接不会更新输入字段。当我更改输入字段时,模型不会更新。
我一直在 jsFiddle 上修改 this on jsFiddle,但无济于事。事实上,似乎数据链接插件实际上并没有按照记录工作(除非我犯了错误)。
如果数据链路不适合此目的,我很乐意提供替代方案的建议。
我将不胜感激您的想法和反馈。
Is it possible to use Backbone.js
and jquery.datalink.js
together to link a Backbone model to a set of fields, and if so how?
For example, given:
<div id="person">
<label for="name">Name:</label>
<input type="text" name="name" id="name" />
</id>
and
var m = Backbone.Model.extend({});
It seems to make sense (but not work) that one could:
$("#person").link(m);
By "not work" I mean that when I change the model, the link doesn't update the input fields. When I change the input fields, the model isn't updated.
I've been tinkering with this on jsFiddle, but to no avail. Indeed, it seems the data link plugin doesn't actually work as documented (unless I've made an error).
If datalink isn't suitable for this, I'd appreciate suggestions for alternatives.
I'd be grateful for thoughts and feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有使用 Datalink 插件,但这可能会有所帮助...
这种违反了封装原则,但您可以在 Backbone 模型内使用
model.attributes
对象。所以也许是这样的:
另外,当你实例化一个 Backbone.Model 时,它看起来像这样:
当你调用 Backbone.Model.extend 时,你实际上创建了一个新的构造函数(就像一个新类),而不是一个对象实例。
I have not used the Datalink plugin but this may help...
This sort of violates the principle of encapsulation but you could use the
model.attributes
object inside the Backbone model.So maybe something like this:
Also, when you instantiate a Backbone.Model it look like this:
When you call
Backbone.Model.extend
you actually create a new constructor (like a new class), not an object instance.@Sam 提供的解决方案预计可以工作,但它存在您必须注意的相关问题。骨干模型是可观察的,即。您可以绑定函数来更改模型中的事件。这些事件由 Backbone 提供的包装器 getter 和 setter 函数触发。如果直接使用 model.attributes 绕过这些 getter 和 setter,那么当数据更改时将不会触发事件。
因此,如果您的数据链接关联是单向的,则这不是问题。当javascript对象被修改时,视图会发生变化,但是如果你想实现双向数据链接,你会遇到问题,因为当用户操作视图时,模型属性确实会被修改,但相应的setter会被修改。未使用,因此应用程序代码的其余部分不会收到更改通知。这可能会导致难以调试的问题。
如果数据绑定是您最关心的问题,请研究一下 KnockoutJS,它具有最先进的数据链接功能。
The solution provided by @Sam is expected to work, but it has associated issues that you must be aware of. Backbone models are observable, ie. you can bind functions to change events in models. These events are triggered by the wrapper getter and setter functions provided by Backbone. If you bypass these getters and setters, by using model.attributes directly then the events will not be triggered when the data changes.
So, this is not a problem if your data-linking association is uni-directional ie. when the javascript object gets modified then the view changes, but if you want to implemenet bi-directional data-linking you will run into problems, because when the view gets manipulated by the user, the model attributes do get modified but the corresponding setters are not used and hence rest of application code is not notified of the change. This can lead to hard to debug problems.
If data binding is your chief concern, do look into KnockoutJS which has state-of-the-art data linking capabilities.