Skip to content

Instantly share code, notes, and snippets.

@chancancode
Last active August 29, 2015 14:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chancancode/5d7f28e83394a1d778ba to your computer and use it in GitHub Desktop.
Save chancancode/5d7f28e83394a1d778ba to your computer and use it in GitHub Desktop.
var ViewHandlerMixin = Ember.Mixin.create({
handles: [],
concatenatedProperties: ['handles'],
views: null,
init: function(){
this.set('views', Ember.Object.create());
this._super();
},
hasHandle: function(name){
return this.get('handles').indexOf(name) > -1;
},
connectHandle: function(handle, obj){
if(this.hasHandle(handle)){
this.set('views.' + handle, obj);
}
},
disconnectHandle: function(handle){
if(this.hasHandle(handle)){
this.set('views.' + handle, undefined);
}
}
});
Ember.ControllerMixin.reopen(ViewHandlerMixin);
Ember.Component.reopen(ViewHandlerMixin);
Ember.View.reopen({
handle: null,
didInsertElement: function(){
this._super();
var handle = this.get('handle'),
target = this.get('targetObject');
if(handle && ViewHandlerMixin.detect(target)){
target.connectHandle(handle, this);
}
},
willDestroyElement: function(){
var handle = this.get('handle'),
target = this.get('targetObject');
if(handle && ViewHandlerMixin.detect(target)){
target.disconnectHandle(handle);
}
this._super();
}
});
App.MyMapComponent = Ember.Component.extend({
/* public API for modifying map state */
panTo: function(lat, lng, zoom, options){
...
},
panBy: function(x, y, zoom, options){
...
},
...
});
App.UnitsController = Ember.ArrayController.extend({
handles: ['map'],
sidebarOpened: false,
actions: {
openSidebar: function(){
this.set('sidebarOpened', true);
this.get('views.map').panBy(-SIDEBAR_WIDTH / 2, 0);
},
closeSidebar: function(){
this.set('sidebarOpened', false);
this.get('views.map').panBy(SIDEBAR_WIDTH / 2, 0);
},
unitSelected: function(unit){
this.get('views.map').panTo(unit.get('lat'), unit.get('lng'));
}
}
});
{{#my-map handle="map"}}
{{#each controller}}
{{#my-map-marker lat=lat lng=lng color=markerColor}}displayName{{/my-map-marker}}
{{/each}}
{{/my-map}}
{{#if sidebarOpened}}
<div class="sidebar">
<button {{action "closeSidebar"}}>X</button>
<ul>
{{#each controller}}
<li class="unit" {{action "unitSelected" this}}>
{{displayName}}
</li>
{{/each}}
</ul>
</div>
{{else}}
<button {{action "openSidebar"}}>Show Sidebar</button>
{{/if}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment