In my current project (Ember 1.10 at present with Ember Data 1.0.0-beta.16), I am caching some Ember Data objects' attributes on their controllers. I am trying to avoid caching on the objects themselves, since I recall this not being recommended.
However, I am having trouble accessing the ObjectControllers from a controller further up the view hierarchy.
I have tried to boil down the issue in the simple JSFiddle (http://ift.tt/1D8UHTh), without Ember Data. I would like a way to access the ObjectControllers for the things from the IndexController. I have tried to use "needs" in the IndexController to import the ThingsController, but it doesn't seem to return a populated controller.
All thoughts gratefully appreciated, and I am open to answers of "you shouldn't need to do it this way and should refactor!"
Many thanks,
Andrew
<script type="text/x-handlebars">
<h1>Hello</h1>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
<p>Here is a list of things:</p>
{{render 'things' this.model}}
<button {{action 'change'}}>Change things</button>
<button {{action 'test'}}>Test access to controllers</button>
</script>
<script type="text/x-handlebars" data-template-name="things">
{{#each thing in this}}
{{render 'thing' thing}}
{{/each}}
</script>
<script type="text/x-handlebars" data-template-name="thing">
<div>
{{text}} ({{originalText}})
</div>
</script>
App = Ember.Application.create();
App.Router.map(function(){});
App.IndexRoute = Ember.Route.extend({
model: function () {
return Ember.A([thing1, thing2]);
}
});
App.Thing = Ember.Object.extend({
text: ''
});
App.IndexController = Ember.Controller.extend({
needs: 'things'
actions: {
change: function(){
this.set('model.firstObject.text', 'Hello2');
this.set('model.lastObject.text', 'Goodbye2');
},
test: function(){
console.log(this.get('model.firstObject.text'));
console.log(this.get('model.firstObject.originalText'));
console.log(this.get('model.lastObject.text'));
console.log(this.get('model.lastObject.originalText'));
console.log(this.get('controllers.things');
}
}
});
App.ThingsController = Ember.ArrayController.extend({});
App.ThingController = Ember.ObjectController.extend({
originalText: '',
cacheOriginalText: function(){
this.set('originalText', this.get('text'));
}.on('init'),
});
var thing1 = App.Thing.create({text: 'Hello'});
var thing2 = App.Thing.create({text: 'Goodbye'});
Aucun commentaire:
Enregistrer un commentaire