Created
June 13, 2014 17:36
-
-
Save macu/9074f9fe70f804d33faa to your computer and use it in GitHub Desktop.
Example of dynamically generating properties on objects, demonstrating that the bindings work as expected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>EmberJS demo</title> | |
| <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js"></script> | |
| <script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.js"></script> | |
| <script src="//cdnjs.cloudflare.com/ajax/libs/ember.js/1.5.1/ember.min.js"></script> | |
| </head> | |
| <body> | |
| <script type="text/x-handlebars"> | |
| Application | |
| <hr> | |
| {{outlet}} | |
| </script> | |
| <script type="text/x-handlebars" data-template-name="index"> | |
| Index | |
| <hr> | |
| {{myProp}} | |
| <button {{action 'enterVal'}}>Enter new value</button> | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var App = Ember.Application.create({}); | |
| App.DynamicPropertiesController = Ember.Controller.extend({ | |
| unknownProperty: function(key) { | |
| // Add a new property to this controller. | |
| Ember.defineProperty(this, key, Ember.computed(function(){ | |
| return key + '(new val)'; | |
| }).property()); | |
| // Listen for changes. | |
| Ember.addObserver(this, key, this, function(sender, key) { | |
| console.log(key, 'changed to', this.get(key)); | |
| }); | |
| // Update the new value on this controller after a second. | |
| setTimeout(function(){ | |
| this.set(key, 'updated'); | |
| }.bind(this), 1000); | |
| // Return the initial value to the requester. | |
| return this.get(key); | |
| }, | |
| }); | |
| App.IndexController = Ember.Controller.extend({ | |
| needs: ['dynamicProperties'], | |
| myPropBinding: 'controllers.dynamicProperties.testing', | |
| actions: { | |
| enterVal: function() { | |
| var newVal = window.prompt('Enter a new value'); | |
| if (Ember.typeOf(newVal) === 'string') { | |
| this.set('myProp', newVal); | |
| } | |
| }, | |
| }, | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment