Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save code0100fun/fa333f111a1553081d81bc2526e7fad6 to your computer and use it in GitHub Desktop.
Save code0100fun/fa333f111a1553081d81bc2526e7fad6 to your computer and use it in GitHub Desktop.
proxying solution
import Ember from 'ember';
export default Ember.Controller.extend({
userService: Ember.inject.service('user'),
userController: Ember.inject.controller('user'),
actions: {
incrementController() {
this.incrementProperty('userController.counter', 1);
},
decrementController() {
this.decrementProperty('userController.counter', 1);
},
incrementService() {
this.incrementProperty('userService.counter', 1);
},
decrementService() {
this.decrementProperty('userService.counter', 1);
},
incrementServiceAlias() {
this.incrementProperty('userService.counterAlias', 1);
},
decrementServiceAlias() {
this.decrementProperty('userService.counterAlias', 1);
},
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
counter: 1,
});
import Em from 'ember';
export default Em.Mixin.create({
unknownProperty: function(key) {
return this.get('proxy').get(key);
},
init() {
this._super(...arguments);
var controller = this.get('proxy');
for (var method in controller) {
if (typeof controller[method] === 'function') {
if (!this[method]) {
this.proxyMethod(method);
}
}
}
},
proxyMethod: function(method) {
this[method] = function() {
var controller = this.get('proxy');
return controller[method](...arguments);
};
},
});
import Ember from 'ember';
import ProxyMixin from '../mixins/proxy';
export default Ember.Controller.extend(ProxyMixin, {
proxy: Ember.computed(function() {
return this.container.lookup('controller:user');
}),
counterAlias: Ember.computed.alias('proxy.counter'),
});
<h1>Proxy Mixin Bugs</h1>
<h2>Counter</h2>
<p>userController.counter: {{userController.counter}}</p>
<p>userService.counter: {{userService.counter}}</p>
<p>userService.counterAlias: {{userService.counterAlias}}</p>
<h3>User controller counter directly</h3>
<div>
<button {{action 'incrementController'}}>Increment</button>
<button {{action 'decrementController'}}>Decrement</button>
</div>
<h3>User service proxy to user controller</h3>
<div>
<button {{action 'incrementService'}}>Increment</button>
<button {{action 'decrementService'}}>Decrement</button>
</div>
<h3>User service alias to user controller</h3>
<div>
<button {{action 'incrementServiceAlias'}}>Increment</button>
<button {{action 'decrementServiceAlias'}}>Decrement</button>
</div>
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "1.13.13",
"ember-template-compiler": "1.13.13",
"ember-testing": "1.13.13"
},
"addons": {
"ember-data": "1.13.15"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment