简体   繁体   English

在 backbone.js 中更改视图时自动更改 model 字段

[英]change model fields automatically when view changed in backbone.js

I have a pop up with plenty of options which can be edited - this is a Backbone View.我有一个弹出窗口,其中有很多可以编辑的选项 - 这是一个主干视图。 I have a model which is simple POCO that stores those options.我有一个 model,它是存储这些选项的简单 POCO。 It's quite hard to write each view event handler to update the model's corresponding values.很难编写每个视图事件处理程序来更新模型的相应值。

How do I update the model automatically when its view fields change?如何在 model 的视图字段更改时自动更新它?

Assume I have textbox Name which corresponds to model.name .假设我有对应于model.name的文本框Name I need to update model.name automatically when I enter a new name in the textbox.当我在文本框中输入新名称时,我需要自动更新model.name Is it possible?可能吗?

I know that this way will not be mvc, but mvvm, and maybe it's better to use knockout.js here, but I already have some code for backbone, so hope it is possible to find some workaround.我知道这种方式不会是 mvc,而是 mvvm,也许在这里使用 knockout.js 会更好,但我已经有了一些骨干代码,所以希望可以找到一些解决方法。

You can manage your form to model updates with Backbone Forms .您可以使用Backbone Forms管理您的表单到 model 更新。

This plugin will allow you to update the model automatically when related view fields have changes.此插件将允许您在相关视图字段发生更改时自动更新 model。

From inside the view, you'd simply redeclare it with this.从视图内部,您只需用它重新声明它。 this.model = SomeOtherModel;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM