mardi 3 février 2015

How to update function in attribute in ember?

I am trying to update an attribute, which is a function, to set pins in a bing maps api.


I want to pin the items that have the boolean attribute "setLocation" set to true. This is working if I hard code the boolean value, but if the value is changed in the application, the pins do not get updated in the map.


This is the html:



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ember Starter Kit</title>
<link rel="stylesheet" href="http://ift.tt/1x2iORp">
<script src="http://ift.tt/1mWWrGH"></script>
<script src="http://ift.tt/1ryZ1pe"></script>
<script src="http://ift.tt/1HQ7eLT"></script>
<script charset="UTF-8" type="text/javascript" src="http://ift.tt/TlYfMQ"></script>
</head>
<body>

<script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>

{{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="index">
{{#each id in model}}
{{input type='checkbox' checked=id.setLocation}}Location{{id.id}}
{{/each}}
{{bing-map height=590 pins=model}}
</script>


</body>
</html>


The JavaScript:



App = Ember.Application.create();

App.Router.map(function() {
// put your routes here
});

App.IndexRoute = Ember.Route.extend({
model: function() {
return App.COORD;
}
});

App.COORD=[
{
id:1,
latitude: 34.05,
longitude: -118.25,
setLocation: true
},
{
id:2,
latitude: 25.77,
longitude: -80.19,
setLocation: true
},
{
id:3,
latitude: 28.53,
longitude: -81.37,
setLocation: true
}
];

App.BingMapComponent = Ember.Component.extend({
attributeBindings: ['style'],
classNames: ['bing-map'],
bingKey: "AkmMkZ5YpX8xsXHY4uBxD8Gz2S5f3GkTRebOw0t4voyb7gFryc0ElW4toY3cJbTt",
width: '45%',
height: '100%',
latitude: 0,
longitude: 0,
latitudePin:undefined,
longitudePin:undefined,
zoom: 1,
pins: null, // passed in from controller
mapTypeId: 'r', // r:road, a:aerial


init: function(){
this._super();
if (!this.get('bingKey')){
throw('Missing bingKey');
}
this.api = Microsoft.Maps;
this.map = null;
},

style: function(){
return "position: relative; width: %@px; height: %@px".fmt(
this.get('width'),
this.get('height')
);
}.property('width', 'height'),

center: function(){
var latitude = parseFloat(this.get('latitude'));
var longitude = parseFloat(this.get('longitude'));
longitude = this.api.Location.normalizeLongitude(longitude);

return new this.api.Location(latitude, longitude);
}.property('latitude', 'longitude'),

mapOptions: function(){
return {
center: this.get('center'),
zoom: parseInt(this.get('zoom'),10),
mapTypeId: this.get('mapTypeId')
};
}.property('center','zoom','mapTypeId'),

createMap: function(){
var el = this.$()[0];
var options = this.get('mapOptions');
options.credentials = this.get('bingKey');
this.map = new Microsoft.Maps.Map(el, options);

var getPin = this.get('getPin');

for(var i=0; i<getPin.length; i++){
var pin = new Microsoft.Maps.Pushpin(getPin[i]);
this.map.entities.push(pin);
}


}.on('didInsertElement'),

getPin: function(){

var pins = this.get('pins');
var location=[];

pins.forEach(function(pin){
if(pin.setLocation){
location.push(new Microsoft.Maps.Location(pin.latitude, pin.longitude));
}
});
return location;
}.property('pins'),

removeMap: function(){
this.map.dispose();
}.on('willDestroyElement'),

});


Thanks in advance





Aucun commentaire:

Enregistrer un commentaire