0

Both functions here return 'undefined'. I can't figure out what's the problem.. It seems so straight-forward??

In the controller I set some properties to present the user with an empty textfield, to ensure they type in their own data.

Amber.ProductController = Ember.ObjectController.extend ({
    quantity_property: "",
    location_property: "",
    employee_name_property: "",

//quantitySubtract: function() {
//return this.get('quantity') -= this.get('quantity_property');
//}.property('quantity', 'quantity_property')

  quantitySubtract: Ember.computed('quantity', 'quantity_property', function() {
    return this.get('quantity') - this.get('quantity_property');
  });
});

Inn the route, both the employeeName and location is being set...

Amber.ProductsUpdateRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('product', params.product_id);
  },
//This defines the actions that we want to expose to the template
  actions: {
    update: function() {
      var product = this.get('currentModel');
      var self = this; //ensures access to the transitionTo method inside the success (Promises) function
  /*  The first parameter to 'then' is the success handler where it transitions
      to the list of products, and the second parameter is our failure handler:
      A function that does nothing.  */
      product.set('employeeName', this.get('controller.employee_name_property'))
      product.set('location', this.get('controller.location_property'))
      product.set('quantity', this.get('controller.quantitySubtract()'))
      product.save().then(
        function() { self.transitionTo('products') },
        function() { }
      );
    }
  }
});

Nothing speciel in the handlebar

<h1>Produkt Forbrug</h1>
<form {{action "update" on="submit"}}>
   ...
<div>
  <label>
  Antal<br>
  {{input type="text" value=quantity_property}}
  </label>
  {{#each error in errors.quantity}}
    <p class="error">{{error.message}}</p>
  {{/each}}
</div>
<button type="update">Save</button>
</form>
UnknownFrequency
  • 413
  • 3
  • 18
  • I don't see you ever setting those properties. Where does that happen? – GJK Oct 28 '14 at 15:56
  • in product.set('val', this.get('value.from.form.params') in the route. as I mentioned, the othr two are are set correctly, only the computed 'quantity' value refuses to get set :) – UnknownFrequency Oct 28 '14 at 16:04
  • That's setting a value on the model, not the controller. After double checking, your computed property syntax is fine, so if it's giving you `undefined`, it's because either `quantity` or `quantity_property` isn't of the right type. – GJK Oct 28 '14 at 16:26
  • I guess it's because 'quantity''s value from the database is not in the controllers scope? Because 'quantity' is set in the route, and quantity_property is set in the controller – UnknownFrequency Oct 28 '14 at 17:00

2 Answers2

0

get rid of the ()

product.set('quantity', this.get('controller.quantitySubtract'))

And this way was fine:

quantitySubtract: function() {
  return this.get('quantity') - this.get('quantity_property');
}.property('quantity', 'quantity_property')

Update:

Seeing your route, that controller wouldn't be applied to that route, it is just using a generic Ember.ObjectController.

Amber.ProductController would go to the Amber.ProductRoute

Amber.ProductUpdateController would go to the Amber.ProductUpdateRoute

If you want to reuse the controller for both routes just extend the product controller like so.

Amber.ProductController = Ember.ObjectController.extend ({
  quantity_property: "",
  location_property: "",
  employee_name_property: "",

  quantitySubtract: function() {
    return this.get('quantity') - this.get('quantity_property');
  }.property('quantity', 'quantity_property')
});

Amber.ProductUpdateController = Amber.ProductController.extend();
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • unfortunately this did not help neither, though I'm sure that was an error too! – UnknownFrequency Oct 28 '14 at 16:06
  • See the update, you are getting a generic controller for the route since it doesn't match the route's name. – Kingpin2k Oct 28 '14 at 17:45
  • it worked for all the values.. location_property, employee_name_property and quantity_property.. all those values were set and would be saved to the db... I still dont known why I couldn't return a computed value.. I guess 'quantity' was always undefined for some reason – UnknownFrequency Oct 28 '14 at 18:06
0

I ended up skipping the function and instead do this:

product.set('quantity', 
  this.get('controller.quantity') - this.get('controller.quantity_property'))

I still dont understand why I could not use that function.. I also tried to rename the controller.. but that was not the issue.. as mentioned before the other two values to fetches to the controller...

Anyways, thanks for trying to help me!

UnknownFrequency
  • 413
  • 3
  • 18