lundi 18 janvier 2016

Ember 1.13 Setting an observed property via a service causes the observing function to run twice

I have an upload component, which uploads a list of selected files when the property allowUpload is set to true.

File Upload Component:

export default EmberUploader.FileField.extend({
  allowUpload: Ember.computed.alias('jobsService.allowUpload'),

  proceedWithUpload: function(files) {
    //Code that uploads files which have been selected.    
  }.observes('allowUpload'),
});

I have created a separate button which sends an action to the controller, and sets the 'allowUpload' property to true.

Template:

<button class='upload-data' {{action 'uploadFiles'}}>Upload Files</button>

Controller:

export
default Ember.Controller.extend({
  jobsService: Ember.inject.service('jobs'),
  allowUpload: Ember.computed.alias('jobsService.allowUpload'),
  
  actions: {
    uploadFiles: function() {
      this.set('allowUpload', true);
    },
  }

});

I am using the jobs service to communicate the setting of the allowUpload property from the controller to the component.

It works, but for some reason the code which observes allowUpload runs twice.

However if I set allowUpload to true within the component itself (With no use of the service), the code runs once as it should.

How can I prevent the code from running twice while using a service to update allowUpload?




Aucun commentaire:

Enregistrer un commentaire