Reputation: 11
This is what I have in a template:
{{check-box isChecked=hasPhoneForSecureAlerts visibleLabelLoc=".t.settingscontact.label.phone" class="checkbox-inline narrow" testId="chk-phone"}}
This is what I have in controller for this property:
hasPhoneForSecureAlerts: Em.computed.bool('securePreferences.phoneLocalNumber'),
Let's assume, that originally this property is set to true and check box is checked, If I uncheck check box, this property is set to false and actually does not correspond to state of securePreferences.phoneLocalNumber.
To set it to initial value, when we are leaving this screen without saving, I just use in resetController at corresponding route:
controller.set('hasPhoneForSecureAlerts', controller.get('securePreferences.phoneLocalNumber') !== null);
Is this code correct? It actually works fine, but I am finding anywhere in Emberjs documentation something like that. Thanks!
Upvotes: 1
Views: 65
Reputation: 1234
I'm not 100% sure what you're code looks like but I think your problem is related to using Em.computed.bool
. In this situation I would use Ember.computed.alias
instead and I created a JSBin to demonstrate that it works. Let me know if you're still having trouble.
EDIT:
Ok, I think I realize now what you're trying to do. As you've noticed, this does work, but I would maybe rethink the way you're going about this simply because a checkbox is designed to be interacted with and what you are doing seems very unnatural. I would instead use a separate variable for your checkbox and make it a property. That way your property()
function will run anytime the checkbox changes value. If the checkbox changes from checked to unchecked, then you can clear the phone number you have stored. I'm not sure what you would want to happen when the checkbox goes from unchecked to checked though. This is why I feel using a checkbox is a somewhat strange decision. Maybe you could create a minimal jsFiddle to demonstrate what you're intending to happen?
Upvotes: 1