Javascript (ember.js)Get Binding属性返回未定义
代码来自ember.js官方介绍:Javascript (ember.js)Get Binding属性返回未定义,javascript,ember.js,Javascript,Ember.js,代码来自ember.js官方介绍: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title></title> </head> <body> <script type="text/javascri
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="ember-0.9.3.js"></script>
<script type="text/javascript">
var App = Ember.Application.create();
App.president = Ember.Object.create({
name: "Barack Obama"
});
App.country = Ember.Object.create({
presidentNameBinding: "App.president.name"
});
App.country.get("presidentName");
</script>
</body>
</html>
var App=Ember.Application.create();
App.president=Ember.Object.create({
姓名:“巴拉克·奥巴马”
});
App.country=Ember.Object.create({
presidentNameBinding:“App.president.name”
});
App.country.get(“主席姓名”);
我试图显示App.country.get(“presidentName”)的返回值代码>,所以我用alert包装了它,但是alert总是显示undefined
。有线部分是,如果我在firebug控制台中执行此语句,它将正确显示,即“Barack Obama”
官方介绍提到:
请注意,绑定不会立即更新。余烬等待,直到所有的
同步之前,应用程序代码已完成运行
变化
这就是我无法在代码中获取binding属性值的原因吗?
“应用程序代码已完成运行”实际上是什么意思?您需要添加一个观察者,该观察者基本上在初始应用程序代码运行后触发
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="js/ember-0.9.3.js"></script>
<script type="text/javascript">
var App = Ember.Application.create();
App.president = Ember.Object.create({
name: "Barack Obama"
});
App.country = Ember.Object.create({
presidentNameBinding: "App.president.name"
});
App.country.addObserver('presidentName', function() {
alert(App.country.get("presidentName"));
});
</script>
</body>
</html>
var App=Ember.Application.create();
App.president=Ember.Object.create({
姓名:“巴拉克·奥巴马”
});
App.country=Ember.Object.create({
presidentNameBinding:“App.president.name”
});
App.country.addObserver('presidentName',function(){
警报(App.country.get(“presidentName”);
});
在中查找“addObserver”。当您处理具有类似绑定的对象时,必须手动触发绑定以与
Ember.run.sync()同步:
下面是一个JSFIDLE示例:
var App = Ember.Application.create();
App.president = Ember.Object.create({
name: "Barack Obama"
});
App.country = Ember.Object.create({
presidentNameBinding: "App.president.name"
});
Ember.run.sync(); // Manually sync bindings
alert( App.country.get("presidentName") );