綁定是Ember.js的一個強大功能,它有助于在兩個屬性之間創(chuàng)建鏈接,如果其中一個屬性更改,另一個屬性會自動更新。您也可以綁定相同的對象或不同的對象。
CarBuyer = Ember.Object.create({ TotalPrice: 860600 }); MarutiZen = Ember.Object.extend({ TotalPrice: Ember.computed.alias('CarBuyer.TotalPrice') }); zen = MarutiZen.create({ CarBuyer: CarBuyer });
上面的代碼描述了如何綁定兩個屬性,如 CarBuyer和MarutiZen 。如果 MarutiZen 更新,它將反映在 CarBuyer 中。
<!DOCTYPE html> <html> <head> <title>Emberjs Bindings</title> <!-- CDN's--> <script src="/attachements/w3c/handlebars.min.js"></script> <script src="/attachements/w3c/jquery-2.1.3.min.js"></script> <script src="/attachements/w3c/ember.min.js"></script> <script src="/attachements/w3c/ember-template-compiler.js"></script> <script src="/attachements/w3c/ember.debug.js"></script> <script src="/attachements/w3c/ember-data.js"></script> </head> <body> <script type="text/javascript"> CarBuyer = Ember.Object.create({ //primary value TotalPrice: 860600 }); MarutiZen = Ember.Object.extend({ //Giving Alias to the dependents TotalPrice: Ember.computed.alias('CarBuyer.TotalPrice') }); //create the object of MarutiZen zen = MarutiZen.create({ CarBuyer: CarBuyer }); document.write('Before Value: '+zen.get('TotalPrice')); // Car price gets raise. zen.set('TotalPrice', 930000); //Effects the CarBuyer document.write('After Updating any one MarutiZen Value: '+CarBuyer.get('TotalPrice')); </script> </body> </html>
讓我們執(zhí)行以下步驟來了解上述代碼的工作原理:
將以上代碼保存在 bindings.html 文件中。
在瀏覽器中打開此HTML文件。
更多建議: