Skip to content

Commit

Permalink
Merge branch 'minor'
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Jun 6, 2014
2 parents 3c80a6d + ac1ca97 commit 74d3151
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 50 deletions.
78 changes: 39 additions & 39 deletions map/doc/prototype.compute-attr.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ with [can.Map::bind bind].

The following example makes a `fullName` attribute on `Person` maps:

var Person = can.Map.extend({
fullName: can.compute(function(){
return this.attr("first")+" "+this.attr("last")
})
})
var Person = can.Map.extend({
fullName: can.compute(function(){
return this.attr("first") + " " + this.attr("last")
})
})

var me = new Person({first: "Justin", last: "Meyer"})
var me = new Person({first: "Justin", last: "Meyer"})

me.attr("fullName") //-> "Justin Meyer"
me.attr("fullName") //-> "Justin Meyer"

me.bind("fullName", function(ev, newValue, oldValue){
newValue //-> Brian Moschel
oldValue //-> Justin Meyer
})
me.bind("fullName", function(ev, newValue, oldValue){
newValue //-> Brian Moschel
oldValue //-> Justin Meyer
})

me.attr({first: "Brian", last: "Moschel"})
me.attr({first: "Brian", last: "Moschel"})

## Getter / Setter computes

Expand All @@ -42,26 +42,25 @@ used to set the compute-property's value.

The following makes `fullName` able to set `first` and `last`:

var Person = can.Map.extend({
fullName: can.compute(function(newValue){
if( arguments.length ) {
var parts = newValue.split(" ");
this.attr({
first: parts[0],
last: parts[1]
});
var Person = can.Map.extend({
fullName: can.compute(function(newValue){
if( arguments.length ) {
var parts = newValue.split(" ");
this.attr({
first: parts[0],
last: parts[1]
});
} else {
return this.attr("first") + " " + this.attr("last");
}
})
})

} else {
return this.attr("first")+" "+this.attr("last");
}
})
})
var me = new Person({first: "Justin", last: "Meyer"})

var me = new Person({first: "Justin", last: "Meyer"})

me.attr("fullName", "Brian Moschel")
me.attr("first") //-> "Brian"
me.attr("last") //-> "Moschel"
me.attr("fullName", "Brian Moschel")
me.attr("first") //-> "Brian"
me.attr("last") //-> "Moschel"


## Alternatives
Expand All @@ -70,17 +69,18 @@ The following makes `fullName` able to set `first` and `last`:
read in the template to a can.compute. So, simply having a fullName
function like:

var Person = can.Map.extend({
fullName: function(){
return this.attr("first")+" "+this.attr("last")
}
})
var me = new Person({first: "Justin", last: "Meyer"})
var Person = can.Map.extend({
fullName: function(){
return this.attr("first") + " " + this.attr("last")
}
})
var me = new Person({first: "Justin", last: "Meyer"})

Will already be live-bound if read in a template like:

{{me.fullName}}
<%= me.attr("fullName") %>
{{me.fullName}}
// or
<%= me.attr("fullName") %>

The [can.Map.setter setter] plugin can also provide similar functionality as
Getter/Setter computes.
Getter/Setter computes.
5 changes: 1 addition & 4 deletions view/mustache/mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -1866,10 +1866,7 @@ steal('can/util',
* {{/unless}}
*/
'unless': function (expr, options) {
var fn = options.fn;
options.fn = options.inverse;
options.inverse = fn;
return Mustache._helpers['if'].fn.apply(this, arguments);
return Mustache._helpers['if'].fn.apply(this, [can.isFunction(expr) ? can.compute(function() { return !expr(); }) : !expr, options]);
},

// Implements the `each` built-in helper.
Expand Down
12 changes: 11 additions & 1 deletion view/mustache/mustache_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,10 @@ steal("can/model", "can/view/mustache", "can/test", "can/view/mustache/spec/spec
expected: "Andy is missing!",
data: {
name: 'Andy'
}
},
liveData: new can.Map({
name: 'Andy'
})
};

var expected = t.expected.replace(/&quot;/g, '&#34;')
Expand All @@ -533,6 +536,13 @@ steal("can/model", "can/view/mustache", "can/test", "can/view/mustache/spec/spec
text: t.template
})
.render(t.data), expected);

// #1019 #unless does not live bind
var div = document.createElement('div');
div.appendChild(can.view.mustache(t.template)(t.liveData));
deepEqual(div.innerHTML, expected, '#unless condition false');
t.liveData.attr('missing', true);
deepEqual(div.innerHTML, '', '#unless condition true');
});

test("Handlebars helper: each", function () {
Expand Down
5 changes: 1 addition & 4 deletions view/stache/mustache_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ steal("can/util", "./utils.js","can/view/live",function(can, utils, live){
}
},
'unless': function (expr, options) {
var fn = options.fn;
options.fn = options.inverse;
options.inverse = fn;
return helpers['if'].apply(this, arguments);
return helpers['if'].apply(this, [can.isFunction(expr) ? can.compute(function() { return !expr(); }) : !expr, options]);
},
'with': function (expr, options) {
var ctx = expr;
Expand Down
14 changes: 12 additions & 2 deletions view/stache/stache_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,22 @@ steal("can/view/stache", "can/view","can/test","can/view/mustache/spec/specs",fu
expected: "Andy is missing!",
data: {
name: 'Andy'
}
},
liveData: new can.Map({
name: 'Andy'
})
};

var expected = t.expected.replace(/&quot;/g, '&#34;')
.replace(/\r\n/g, '\n');
deepEqual(getText(t.template,t.data), expected);
deepEqual(getText(t.template, t.data), expected);

// #1019 #unless does not live bind
var div = document.createElement('div');
div.appendChild(can.stache(t.template)(t.liveData));
deepEqual(div.innerHTML, expected, '#unless condition false');
t.liveData.attr('missing', true);
deepEqual(div.innerHTML, '', '#unless condition true');
});

test("Handlebars helper: each", function () {
Expand Down

0 comments on commit 74d3151

Please sign in to comment.