Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can.computes with can.Map.prototype.attr() do not set up bindings #626

Merged
merged 2 commits into from
Jan 7, 2014

Conversation

onyxrev
Copy link
Contributor

@onyxrev onyxrev commented Dec 21, 2013

If I use can.Map.prototype.attr() in a can.compute it doesn't set up a binding like can.Map.prototype.attr("some_property") does. I often use computes to generate dynamic links and I use .attr() to serialize the data in a can.Map for use in creating the URL parameters, for example. You may not know what data will be inside the can.Map, for example, so might want to use .attr() to explode the values out when they change.

Doesn't work:

var listenToMe = new can.Map({ iChange: "fromThis" });

can.compute(function(){
    return listenToMe.attr().iChange;
});

listenToMe.attr("iChange", "toThis");

Does work:

var listenToMe = new can.Map({ iChange: "fromThis" });

can.compute(function(){
    return listenToMe.attr("iChange");
});

listenToMe.attr("iChange", "toThis");

Example of dynamic URL:

can.compute(function(){
    var routeParams = $.extend({}, listenToMe.attr(), { analytics_section_code: "report_builder", report: reportCode });
    return can.route.url(routeParams);
});

@justinbmeyer
Copy link
Contributor

I actually noticed that today while reviewing can.Map's source code. _attrs doesn't call .attr() which calls can.__reading to signal live-binding.

onyxrev added a commit to onyxrev/canjs that referenced this pull request Dec 21, 2013
@onyxrev
Copy link
Contributor Author

onyxrev commented Dec 21, 2013

Maybe a naive fix? I haven't been in this part of the code before but this definitely works.

@justinbmeyer
Copy link
Contributor

Thanks. Anyway you could add a test? Also, can you add a check that it updates if you add a property too? You will need to tell __reading about the __keys event (you can find this happening other places).

Ah, too bad you missed the hangout today where I went over can.Map in detail :-).

@onyxrev
Copy link
Contributor Author

onyxrev commented Dec 21, 2013

Okay - will do.

onyxrev added a commit to onyxrev/canjs that referenced this pull request Dec 21, 2013
onyxrev added a commit to onyxrev/canjs that referenced this pull request Dec 21, 2013
@onyxrev
Copy link
Contributor Author

onyxrev commented Dec 21, 2013

It works on addition and subtraction of properties. Added __keys and test.

onyxrev added a commit to onyxrev/canjs that referenced this pull request Dec 21, 2013
onyxrev added a commit to onyxrev/canjs that referenced this pull request Dec 21, 2013
onyxrev added a commit to onyxrev/canjs that referenced this pull request Dec 21, 2013
daffl added a commit that referenced this pull request Jan 7, 2014
can.computes with can.Map.prototype.attr() do not set up bindings
@daffl daffl merged commit a81c1d7 into canjs:master Jan 7, 2014
@justinbmeyer
Copy link
Contributor

@onyxrev Thanks big time for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants