简体   繁体   English

如何在emberjs上使用单向绑定?

[英]How to use one-way binding on emberjs?

I'm starting to play around with ember but one thing I haven't been able to wrap my head around is how to use one-way bindings, consider the following code: 我开始尝试使用余烬,但我无法解决的一件事是如何使用单向绑定,请考虑以下代码:

HTML 的HTML

<script type="text/x-handlebars">
    <h1>Some App</h1>
    <p>
        My Name is: {{name}}
    </p>
    <form {{action 'updateName' on="submit"}}>
        {{view Ember.TextField valueBinding="name"}}
        <input type="submit" value="Save">
    </form>
</script>

JS JS

var App = Ember.Application.create();

App.ApplicationRoute = Ember.Route.extend({
    model: function() {
        return {
            name: 'John Doe'   
        }
    },
    events: {
        updateName: function() {
            console.log('Name Updated!');   
        }
    }
});

JS Fiddle for reference JS小提琴供参考

By default the Ember.TextField 's value will be bound to my model and viceversa which means that when I type on the text the model and view will be updated live, however what I'm trying to do is bind the model to the textfield (so the initial name will be displayed) but update the model only when the form is submitted. 默认情况下, Ember.TextField的值将绑定到我的模型,反之亦然,这意味着当我在文本上键入时,模型和视图将实时更新,但是我想做的是将模型绑定到文本字段(因此将显示初始名称),但仅在提交表单时更新模型。 Is there an easy way to do it? 有一个简单的方法吗?

Thanks in advance. 提前致谢。

EDIT : Just for reference I updated my fiddle to use Ember.Binding.oneWay I think the end result is cleaner than @c4p answer: http://jsfiddle.net/X2LmC/3/ however I'm not sure if doing $('#user-name').val() to get the field value is the right way to do it. 编辑 :仅供参考,我更新了小提琴以使用Ember.Binding.oneWay我认为最终结果比@ c4p答案更干净: http : //jsfiddle.net/X2LmC/3/但是我不确定是否在做$('#user-name').val()获取字段值是正确的方法。

You can use an observer, an intermediate bound variable on the controller, and an event handler on the controller to accomplish what want to do. 您可以使用观察者,控制器上的中间绑定变量和控制器上的事件处理程序来完成想要做的事情。

The nameOnController property of the controller will be updated when the nameDidChange() observer fires, making sure that the nameOnController property will initialize to the model's name property and reflect any future changes to name . nameDidChange()观察器启动时,将更新控制器的nameOnController属性,确保nameOnController属性将初始化为模型的name属性,并反映对name任何将来更改。 Bind this intermediate property to the TextField to insulate the name property from immediate typing changes and use an event on the controller to read and set the name attribute only when the button is clicked. 将此中间属性绑定到TextField以将name属性与立即键入的更改隔离开,并仅在单击按钮时使用控制器上的事件来读取和设置name属性。

template: 模板:

{{view Ember.TextField valueBinding="nameOnController"}}

JS: JS:

App.ApplicationController = Ember.ObjectController.extend({
    nameOnController: null,

    nameDidChange: function() {
      var name = this.get('name');
      this.set('nameOnController', name); 
    }.observes('name'),

    updateName: function() {
      var controllerName = this.get('nameOnController'); 
      this.set('name', controllerName);
    },

    setName: function() {
      this.set('name', "new name");
    }
});

update JSFiddle example 更新JSFiddle示例

You can check that changes to the name property are still reflected in the text box by clicking the Set New Name button. 您可以通过单击“ Set New Name按钮来检查对name属性的更改是否仍反映在文本框中。

Heres an updated solution. 这是更新的解决方案。

I had a similar problem, that I needed a one way binding, but upon some action I needed the latest value. 我有一个类似的问题,我需要单向绑定,但是在执行某些操作时,我需要最新的价值。 I also needed the current edited value. 我还需要当前的编辑值。 Below is my proof of concept -- 以下是我的概念证明-

http://emberjs.jsbin.com/nocadubixi/edit?html,js,output http://emberjs.jsbin.com/nocadubixi/edit?html,js,输出

Handlebars: 车把

    <h1>Some App</h1>
    <p>My Name is: {{model.name}}</p>
    <p>current value: {{currentName}}</p>

    {{one-way-field id="user-name" source=model.name action='handleNameChange'}}
    <button {{action 'updateName'}}>Save</button>

JS: JS:

App = Ember.Application.create();

App.ApplicationController = Em.Controller.extend({
    model: {
      name: 'John Doe'
    },
    actions: {
      updateName: function() {
        this.set('model.name', this.get('currentName'));
      },
      handleNameChange: function(newValue) {
        this.set('currentName', newValue);
      },

    }
});


App.OneWayFieldComponent = Ember.TextField.extend({
  valueBinding: Ember.Binding.oneWay('source'),
  onValueChange: function() {
    this.sendAction('action', this.get('value'));
  }.observes('value'),
});

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

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