Ember update property on the changes in array


#1

I have following in my controller, and facing issue while updating property with array change… import Ember from ‘ember’;

export default Ember.Controller.extend({

imageIds: Object.keys(JSON.parse(localStorage.image_ids || “{}”)),
// [‘gnffffffffjdf’, ‘hzfyfsidfulknm’, ‘euriekjhfkejh’]

previewImageId: function() { return this.imageIds.get(‘firstObject’);
}.property(‘imageIds.[]’),

noImage: function() { return this.get(‘imageIds.length’) === 0; }.property(‘imageIds.@each’),

actions: { addDetails: function() { this.transitionToRoute(‘items.add_item’); },

removeImage: function(image_id) {
  var uploaded = JSON.parse(localStorage.image_ids || "{}");
  delete uploaded[image_id]
  localStorage.image_ids = JSON.stringify(uploaded);
  this.get("imageIds").removeObject(image_id);
  // this.set("imageIds", Object.keys(JSON.parse(localStorage.image_ids || "{}")));
},

updatePreview: function(image_id){
  this.set("previewImageId", image_id);
  var uploaded = JSON.parse(localStorage.image_ids || "{}");
  uploaded[image_id] = image_id;
  localStorage.image_ids = JSON.stringify(uploaded);
  // this.set("imageIds", Object.keys(JSON.parse(localStorage.image_ids)));
  this.get("imageIds").pushObject(image_id);
}   }

});

Whenever there is any change in the imageIds array, previewImageId should be updated. tried using pushObject, removeObject, .get and .set options. But still no luck

Can anyone pls help me?


#2

You are overriding your computed property right there. You could try making your computed property a setter (Ember.js Docs Guide) or use an observer instead.

Hope that helps.


#3

yes you are correct… updated now… working… thanks :+1: