Ember how to call computed property with arguements - ember.js

checked: ((key, value) ->
selected = #get 'controllers.a.selected'
a = #get 'model'
if arguments.length > 1
if value
selected.addObject a
else
selected.removeObject a
return selected.contains a
).property('controllers.a.selected.length')
Now I am trying to call the checked property.
I tried to use #controller.get('checked'),
but how do I pass key, value arguments so that I can test the property.
I do not know how to call it. Thanks a lot.

I hate setting computed properties, I think it's a terrible pattern, but here's how it's done #controller.set('checked', 'foo').
http://emberjs.com/guides/object-model/computed-properties/#toc_setting-computed-properties

Related

Emberjs: Use args as tracked property

I am doing a store query in the controller and then passing the result down to a child component.
// Controller
#tracked comment;
#action
async fetchComment() {
const comment = await this.store.query('note', {
filter: {
listing: this.listing.id,
},
});
if (comment && comment.length > 0) {
this.comment = comment.firstObject;
}
}
// Template
<MyComponent #comment={{this.comment}} />
I want to use the arg to populate a tracked property in the child component.
// Component
#tracked comment = this.args.comment;
I found this does not work but works well as a getter. However with a getter I am unable to set this to null when deleting that record.
I have also tried to set the tracked property in the Constructor but that didnt work either.
I suspect this has something to do with passing in a promise or store object in the args because this works fine with static data.
why your code does not work
this code can not work:
#tracked comment = this.args.comment;
this is because comment on the controller is initially undefined but will later bet set to comment.firstObject when the network request is done and the await in your fetchComment function returns.
Generally everythings on args basically always behaves like its #tracked (while more accurate you would describe it as getters). So this usually will just update fine. But the assignment #tracked comment = this.args.comment; only happens once when you create the component, so you no longer depend on updates on args.
why you can not set this.args.comment to null
If you use a getter or directly always use this.args.comment you can not change this reference. This is because this.args is always readonly. you can change objects on this.args.something, but you never can change the reference or primitive value on this.args.
Sidenote: this is only true if the component was called with <AngleBracket /> syntax. For the older {{curly-component}} syntax this is not true. So this does not depend on the component itself but how the component gets called.
you could notify the controller to remove the reference
one good thing to do is to pass down a deleteComment action to the component that basically does something like this.comment = null on the controller. then you use this.args.comment directly or by a getter and you can call this.args.deleteComment() to set comment on the controller to null, which will update anything that uses this.args.comment or a getter that returns this.args.comment.
this is essentially because in your architecture the controller owns the data (because it loads it). so the controller is also responsible to delete it.
if you use ember-data you can check isDeleted
if its a ember-data model (which it probably is if you call this.store) then it has a isDeleted property. you can use this to check if the record is deleted, since ember-data records dont disappear if they get deleted. which is exactly because of problems like this.
how you use another property to shadow a argument
you could do something like this to shadow an argument in your component:
#tracked commentIsDeleted = false;
get comment() {
return this.commentIsDeleted
? null
: this.args.comment;
}
this way at first this.comment will work like a normal getter, but you can shadow delete it by setting this.commentIsDeleted = true;. From that on this.comment will be null.

How to check properties attribute types in an Ember Model unit test?

I'm trying to write a utility for testing Ember Models. At the moment I have a function which loops through the properties defined in the model (by getting the keys) and matching them with the expected keys.
I'd like to take this one step further and check that the properties have the correct value in the model. I want to be able to check for DS.attr(string/number/date etc) and Computed properties.
How can I do this?
I've worked this out:
model.eachAttribute((key, meta) => {
const keyType = expected[key];
assert.ok(expected[key], `the "${key}" property is defined.`);
assert.equal(meta.type, keyType, `the "${key}" has the attribute type "${keyType}".`);
});

emberjs - dynamic computed property does not trigger classNameBindings

I am creating a mixin to validate components with texFields it looks like this, I am using Ember.defineProperty to create a cp on the fly with a dynamic dependant key:
App.ValidationMixin = Ember.Mixin.create
classNameBindings: ['isInvalid']
input: (e) ->
#_super.apply this, arguments
setup: Ember.on 'didInsertElement', ->
unless validations = #get('validations')
el = #autocompleteElement()
# I had to add this to access the prop rather than it getting
# triggered when the dynamic property changes
if #get('isInvalid')
el.addClass 'is-invalid'
else
el.removeClass 'is-invalid'
validationMixin: Ember.on 'didInsertElement', ->
unless validations = #get('validations')
return
dynamicProperty = # logic to determine dynamic property
Ember.defineProperty this, 'isInvalid', Ember.computed dynamicProperty, 'validator.isSubmitted', ->
# validation logic
The problem is, I have to manually check for this.get('isInvalid') rather then the property function being executed when one of the dependant keys changes.
Can anyone explain why this is?
Ember probably thinks that the property 'isInvalid' is not used anywhere and therefore doesn't update it. I guess that 'didInsertElement' is called after the classNameBindings property... Try to output the 'isInvalid' property in the template to check if that's the case because therefore it must be updated.

Why doesn't my Ember computed property get a second argument?

I expected a second argument to be passed to my computed property method, but it's not. I need this so I can call a setter to save my model with new data. Instead of that behavior, it appears that my computed property is called again right before I save the model, and clobbering the new values - the setter is never called at all because I only get one argument. Computed property:
changeBananas: function(k, v) {
var bananas = this.get('bananas'), bananaList = [];
console.log('args: ');
console.log(arguments);
bananaList = bananas.map(function(b) {
return { color: b.get('color') };
});
if (arguments.length > 1) {
console.log('I never get called!');
return bananaList;
}
return bananaList;
}.property('bananas.#each')
Full JSBin:
http://jsbin.com/razimaxu/2/edit
I tried propertyWillChange() and friends to try to stop observers, but it did not do anything. Is there another way to do this? My computed property is there to do some formatting of the items before displaying them in editable fields. I expected to be able to change said fields and save just like any other fields that are connected to regular model properties.
The only time it will receive both arguments is if you attempt to set the computed property, such as this.set('changeBananas', []).
It doesn't get called with both arguments if it has noticed a dependent property has changed.

Format a property before displaying it

Hello StackOverflow experts,
I would like to know if it would be possible to use Ember.js' computed properties to modify the value of the property before returning to whatever object requests it.
Imagine this simple example:
I have a User object with mail property
When I set the property, I want the email address to change from first.last#example.com to first.last#anotherexample.com, then return it
When I request the property ( via User.get ) I want to get the modified property back.
I think it should be pretty simple by utilising another 'helper' property, like formatted_mail, where I would store and retrieve the formatted value, but I wonder if something like this can be done without additional model properties.
So far, I have this coffescript code, but I always get 'undefined' when reading the property, even though I set it before, so I suspect the value does not get saved by Ember anywhere:
mail: ( ( key, value ) ->
if arguments.length == 1
return this.get 'mail'
else
return value.split( '#' )[0] + '#anotherexample.com'
).property 'mail'
Thank you for your assistance!
You are close to solution.
As computed properties are always cached by default in Ember (you could disable this behaviour using .volatile()), you do not have to specify what to do when arguments.length is 1, except if you want to specify a default value.
So here it should looks like:
App.User = Ember.Object.extend({
mail: function(key, value) {
if (arguments.length === 2) {
return value.split('#')[0] + "#tieto.com";
}
return null;
}.property()
});
The return null just specify the default value.
When you set the mail property, it will cache the returned value and always returns it without recomputing this property.
Note that you can do that only because the mail property does not depend on other properties. If you were declaring it with .property('anotherProperty'), the mail property will be recomputed any time anoterProperty changes. So in the example above it will reset it to null.
You can try it in this JSFiddle.