SuperNinja
SuperNinja

Reputation: 1596

Set computed property from another controller

I am trying to set the value of a computed property from one controller to another.

var BusinessOwner = Ember.ObjectController.extend({
    actions: {
        save: function(){
            var self = this;
            return Ember.$.ajax({

            }).then(function(){
                var ownerShow = self.store.getById('application',100);
                ownerShow.get('ownerGeneral');
                ownerShow.set('ownerGeneral', 'complete')

                Ember.set(self, 'controllers.collectinfo.ownerGeneral','completed');

                //self.set('controllers.collectinfo.ownerGeneral', "completed");
            });
        }
    }

I have tried several different attempts at setting this property but have proved unsuccessful. If I use the self set, errors that I must use Ember.set(). If I use Ember.set() I get error collectinfo must be global if no obj given.

Thanks for any help

EDIT: Thanks for looking at this. Yes I am includeing needs: 'collectinfo' I am still getting the error that Ember.set() needs to be used to set the object

Upvotes: 0

Views: 1846

Answers (4)

mavilein
mavilein

Reputation: 11668

I assume that your controller declares a needs property with "collectInfo" as value? Then it should work this way:

var BusinessOwner = Ember.ObjectController.extend({
    needs : ['collectInfo'],
    actions: {
        save: function(){
            var collectinfoController = this.get('controllers.collectinfo');
            return Ember.$.ajax({

            }).then(function(){
                var ownerShow = self.store.getById('application',100);
                ownerShow.get('ownerGeneral');
                ownerShow.set('ownerGeneral', 'complete')

                collectinfoController.set('ownerGeneral','completed');
            });
        }
    } 

Upvotes: 0

redOctober13
redOctober13

Reputation: 3974

In addition to what others said about "needs," just declare a shortcut variable for set and get:

var get = Ember.get;
var set = Ember.set;

and then use them like so:

set(object, 'property', 'value-to-set-property-to');

Upvotes: 0

ppcano
ppcano

Reputation: 2861

You can set dependencies between controller with the controller needs property, it's documented at Ember Guide.

App.IndexController = Em.Controller.extend({
  needs: 'application',
  message: 'hi!',
  actions: {
    changeApplicationMessage: function() {
      this.set('controllers.application.message', 'good bye');
    },
    changeMessage: function(){
      this.set('message', 'bye');
    }
  }
});

The dependent controller property will be accesible in the controller at {{controllers.controllerName.propertyName}}

Demo: http://emberjs.jsbin.com/vevet/1/edit

Upvotes: 1

thecodejack
thecodejack

Reputation: 13379

You need to provide needs array in the controller as well.

var BusinessOwner = Ember.ObjectController.extend({
   needs: 'collectinfo'
   actions: {
        save: function(){
            var self = this;
            return Ember.$.ajax({

            }).then(function(){
                var ownerShow = self.store.getById('application',100);
                ownerShow.get('ownerGeneral');
                ownerShow.set('ownerGeneral', 'complete')

                Ember.set(self, 'controllers.collectinfo.ownerGeneral','completed');

                //self.set('controllers.collectinfo.ownerGeneral', "completed");
            });
        }
    }

Coding wise i suggest you create a own computed property for the one you want to access from other controller. So code becomes like this.

var BusinessOwner = Ember.ObjectController.extend({
       needs: 'collectinfo',
       ownerGeneral: Ember.computed.alias('controllers.collectinfo.ownerGeneral')
       actions: {
            save: function(){
                var self = this;
                return Ember.$.ajax({

                }).then(function(){
                    var ownerShow = self.store.getById('application',100);
                    ownerShow.get('ownerGeneral');
                    ownerShow.set('ownerGeneral', 'complete')

                    Ember.set(self, 'ownerGeneral','completed');

                    //self.set('controllers.collectinfo.ownerGeneral', "completed");
                });
            }
        }

Upvotes: 1

Related Questions