目录

link

对象模型绑定通过使用单向绑定方法computed.oneWay()指定一个方向上的更改,并且在通过重写指定另一个属性上的行为时非常有用。

例子 (Example)

以下示例通过覆盖指定另一个属性的行为 -

import Ember from 'ember';
export default function() {
   var CarOne = Ember.Object.create ({
      //primary value
      TotalPrice: 860600
   });
   var Car = Ember.Object.extend ({
      TotalPrice: Ember.computed.oneWay('CarOne.TotalPrice')
   });
   var Car = Car.create ({
      CarOne: CarOne
   });
   //Changing the user object name, changes the value on the view
   CarOne.set('TotalPrice', 860600);
   //Car.TotalPrice will become "860600"
   Car.set('TotalPrice', 930000);  // changes to the view don't make it back to the object.
   document.write('<h3>One Way Binding<h3>');
   document.write('Value of car : ' + CarOne.get('TotalPrice')); //display value as 860600
}

现在打开app.js文件并在文件顶部添加以下行 -

import objectmodelonewaybinding from './objectmodelonewaybinding';

其中, objectmodelonewaybinding是指定为“objectmodelonewaybinding.js”并在“app”文件夹下创建的文件的名称。

接下来在导出之前调用底部的继承“objectmodelonewaybinding”。 它执行在objectmodelonewaybinding.js文件中创建的objectmodelonewaybinding函数 -

objectmodelonewaybinding();

输出 (Output)

运行ember服务器,您将收到以下输出 -

Ember.js对象模型单向绑定
↑回到顶部↑
WIKI教程 @2018