Skip to content

Commit

Permalink
Merge pull request canjs#1330 from bitovi/renderer-deferred-1139
Browse files Browse the repository at this point in the history
Allow renderers to be called with can.view and Deferreds
  • Loading branch information
daffl committed Nov 18, 2014
2 parents fd8888d + 97d9848 commit 5dead09
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
22 changes: 11 additions & 11 deletions view/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ steal('can/util', function (can) {
* @param {Boolean} async If the ajax request should be asynchronous.
* @returns {can.Deferred} a `view` renderer deferred.
*/
var get = function (obj, async) {
var getRenderer = function (obj, async) {
// If `obj` already is a renderer function just resolve a Deferred with it
if(isFunction(obj)) {
var def = can.Deferred();
return def.resolve(obj);
}

var url = typeof obj === 'string' ? obj : obj.url,
suffix = (obj.engine && '.' + obj.engine) || url.match(/\.[\w\d]+$/),
type,
Expand Down Expand Up @@ -191,15 +197,9 @@ steal('can/util', function (can) {
callback = helpers;
helpers = undefined;
}
var result;
// Get the result, if a renderer function is passed in, then we just use that to render the data
if( isFunction(view) ) {
result = view(data, helpers, callback);
} else {
result = $view.renderAs("fragment",view, data, helpers, callback);
}

return result;
// Render the view as a fragment
return $view.renderAs("fragment",view, data, helpers, callback);
};

// can.view methods
Expand Down Expand Up @@ -608,7 +608,7 @@ steal('can/util', function (can) {
dataCopy = can.extend({}, data);

// Add the view request to the list of deferreds.
deferreds.push(get(view, true));
deferreds.push(getRenderer(view, true));
// Wait for the view and all deferreds to finish...
can.when.apply(can, deferreds)
.then(function (resolved) {
Expand Down Expand Up @@ -655,7 +655,7 @@ steal('can/util', function (can) {
// If there's a `callback` function
async = isFunction(callback);
// Get the `view` type
deferred = get(view, async);
deferred = getRenderer(view, async);

if (reading) {
can.__setReading(reading);
Expand Down
18 changes: 18 additions & 0 deletions view/view_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,4 +815,22 @@ steal("can/view/callbacks",
}
ok(pass);
});
test('renderer passed with Deferred gets executed (#1139)', 1, function() {
// See http://jsfiddle.net/a35ZH/1/
var template = can.view.mustache('<h1>Value is {{value}}!</h1>');
var def = can.Deferred();

stop();

setTimeout(function() {
def.resolve({
value: 'Test'
});
}, 50);

can.view(template, def, function (frag) {
equal(frag.textContent, 'Value is Test!');
start();
});
});
});

0 comments on commit 5dead09

Please sign in to comment.