本文介绍了垃圾邮件绑定无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注emberjs.com上的,但无法使第一个绑定示例正常工作。



我创建了一个来演示。我缺少什么?

解决方案

Ember.js使用RunLoop的概念来允许绑定,观察者等。 p>

该示例的问题是通过设置(bound)属性并通过 console.log no触发事件触发RunLoop,从而同步更改。有关RunLoop的2篇优秀博客文章:和。虽然它们的目标是Sproutcore,但Ember.js的概念也是一样的。



有两种方法可以使您的示例工作。



通过



根据文档的状态,调用 Ember.run.sync() ...是一种有用的方法,立即强制应用程序中的所有绑定同步。这会留下这样的代码,请参阅

  App = Ember.Application.create({}); 
App.wife = Ember.Object.create({
householdIncome:80000
});

App.husband = Ember.Object.create({
homeIncomeBinding:'App.wife.householdIncome'
});

//强制绑定同步
Ember.run.sync();

console.log(App.husband.get('homeIncome')); // 80000

//有人加注
App.husband.set('homeIncome',90000);

//强制绑定同步
Ember.run.sync();

console.log(App.wife.get('homeIncome')); // 90000






或第二个选项是...



在视图中显示值



显示属性一个视图处理您所有的RunLoop内容,请参阅



JavaScript

  App = Ember.Application.create({}); 
App.wife = Ember.Object.create({
householdIncome:80000
});

App.husband = Ember.Object.create({
homeIncomeBinding:'App.wife.householdIncome'
});

//调用3000ms的函数
Ember.run.later(function(){
//有人获得加注
App.husband.set('homeIncome ',90000);
},3000);

Handlebars (视图):

 < script type =text / x-handlebars > 
Wifes收入:{{App.wife.householdIncome}}< br />
丈夫收入:{{App.husband.householdIncome}}
< / script>


I am following the documentation on emberjs.com, but can't get the first bindings example to work.

I created a jsfiddle to demonstrate. What am I missing?

解决方案

Ember.js uses the concept of a RunLoop to allow bindings, observers and so on.

The problem with the example is that by setting a (bound) property and immediately getting the value via console.log no event is fired which would trigger the RunLoop and therefore synchronize the changes. There are 2 excellent blog posts about the RunLoop: Part 1 and Part 2. Although they target Sproutcore, the concept is about the same for Ember.js.

There are two ways to make your example work.

Force synchronisation via Ember.run.sync()

As the docs state, invoking Ember.run.sync() ... is a useful way to immediately force all bindings in the application to sync. This leaves the code like this, see http://jsfiddle.net/pangratz666/cwR3P/

App = Ember.Application.create({});
App.wife = Ember.Object.create({
  householdIncome: 80000
});

App.husband = Ember.Object.create({
  householdIncomeBinding: 'App.wife.householdIncome'
});

// force bindings to sync
Ember.run.sync();

console.log(App.husband.get('householdIncome')); // 80000

// Someone gets raise.
App.husband.set('householdIncome', 90000);

// force bindings to sync
Ember.run.sync();

console.log(App.wife.get('householdIncome')); // 90000​


Or the second option is to ...

Show the values in a view

Showing the properties in a view handles all the RunLoop stuff for you, see http://jsfiddle.net/pangratz666/Ub97S/

JavaScript:

App = Ember.Application.create({});
App.wife = Ember.Object.create({
    householdIncome: 80000
});

App.husband = Ember.Object.create({
    householdIncomeBinding: 'App.wife.householdIncome'
});

// invoke function in 3000ms
Ember.run.later(function() {
    // someone gets a raise
    App.husband.set('householdIncome', 90000);
}, 3000);​

Handlebars (the view):

<script type="text/x-handlebars" >
    Wifes income: {{App.wife.householdIncome}}<br/>
    Husbands income: {{App.husband.householdIncome}}
</script>​

这篇关于垃圾邮件绑定无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:23