2012-09-03 23 views
7

Estoy tratando de mostrar un mapa de Google embebido basado en la ubicación de un objeto de evento.Ember.js con Google Map en la vista

Esta es la aplicación básica:

App = Em.Application.create(); 

App.Event = Em.Object.extend({ 
    lat: -33.8665433, 
    lng: 151.1956316 
}); 

App.EventController = Ember.ObjectController.extend(); 

App.ApplicationController = Ember.ObjectController.extend(); 

App.EventView = Ember.View.extend({ 
    templateName: 'event' 
}); 

App.ApplicationView = Ember.View.extend({ 
    templateName: 'application' 
}); 

App.Router = Ember.Router.extend({ 
    enableLogging: true, 
    root: Ember.Route.extend({ 
     event: Ember.Route.extend({ 
      route: '/', 
      connectOutlets: function (router) { 
       router.get('eventController').set('content', App.Event.create()); 
       router.get('applicationController').connectOutlet('event'); 
      } 
     }) 
    }) 
}); 

App.initialize(); 

Con las siguientes plantillas:

<script type="text/x-handlebars" data-template-name="application"> 
    {{outlet}} 
</script> 
<script type="text/x-handlebars" data-template-name="event"> 
    {{lat}} {{lng}} 
    // Embeded Google Map 
</script> 

Dónde podría inicializar el mapa? Además, si lat/lang cambia, ¿cómo puedo atraparlo y volver a dibujar el mapa?

Trabajo Ver Código (modificado de la respuesta de sabithpocker)

App.EventView = Ember.View.extend({ 
    templateName: 'event', 
    map: null, 
    latitudeBinding: 'controller.content.lat', 
    longitudeBinding: 'controller.content.lng', 
    didInsertElement: function() { 
     var mapOptions = { 
      center: new google.maps.LatLng(this.get('latitude'), this.get('longitude')), 
      zoom: 8, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     var map = new google.maps.Map(this.$().get(0), mapOptions); 
     this.set('map', map); //save for future updations 
     this.$().css({ width: "400px", height: "400px" }); 
    }, 
    reRenderMap: function() { 
     if (this.get('map')) { 
      var newLoc = new google.maps.LatLng(this.get('latitude'), this.get('longitude')); 
      this.get('map').setCenter(newLoc); 
     } 
    }.observes('latitude', 'longitude') //these are bound to lat/lng of Event 
}); 

Respuesta

6

Aquí es una idea rápida, no siguiente estructura aplicación brasa, sólo una idea.

App.EventView = Ember.View.extend({ 
    templateName: 'event', 
    map : null, 
    latitudeBinding : 'App.Event.lat', 
    longitudeBinding : 'App.Evet.lng', 
    didInsertElement : function(){ 
     var mapOptions = { 
     center: new google.maps.LatLng(this.get('latitude'), this.get('longitude')), 
     zoom: 8, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     var map = new google.maps.Map(this.$().get(0),mapOptions); 
     this.set('map',map); //save for future updations 
    }, 
    reRenderMap : function(){ 
     var newLoc = new google.maps.LatLng(this.get('latitude'), this.get('longitude')); 
     this.get('map').setCenter(newLoc) 
     }.observes('latitude','longitude') //these are bound to lat/lng of Event 
}); 

También pienso App.Event debería ser:

App.Event = Em.Object.extend({ 
    lat: null, 
    lng: null, 
    init : function(){ 
     this.set('lat', -33.8665433); 
     this.set('lng', 151.1956316); 
     } 
}); 

para evitar el llamado chromosomal mutation in Ember

+0

Muchas gracias! Actualicé la pregunta con el código de vista final (la única diferencia real es enlazar a la propiedad de contenido de eventController). –

+0

Me alegro de que haya ayudado :) – sabithpocker