2012-09-03 11 views
7

私は、埋め込まれたGoogleマップをEventオブジェクトの場所に基づいて表示しようとしています。以下のテンプレートで Ember.js with Google Map with view

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(); 

::私はマップを初期化します

<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> 

これは基本的なアプリですか!さらに、lat/langが変更された場合、どのようにキャッチしてマップを再描画しますか?ここで

ワーキングコードの表示(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 
}); 

答えて

6

は燃えさしアプリの構造、単なるアイデア以下ではない、迅速なアイデアです。

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

いわゆるchromosomal mutation in Ember

+0

はそんなにありがとう避けるために:

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 }); 

また、私はApp.Eventがあるべきだと思います!最後のビューコードで質問を更新しました(実際の違いはeventControllerのcontentプロパティにバインドされているだけです)。 –

+0

それは助けて嬉しい:) – sabithpocker