tbranyen/backbone-boilerplate — Issue #80: Some small modifications in namespace.js
Author: hetsch
Created: 2012-03-08 10:07:44+00:00
State: open

Hi,

While testing my application, I've run into some small problems using the fetch template function.
If I pass no 'done' callback because I'm using the deferred, it throws an error that no done callback is defined.

Following modifications might help:

```javascript
fetchTemplate: function(path, done) {
var JST = window.JST = window.JST || {};
var def = new $.Deferred();

// Should be an instant synchronous way of getting the template, if it
// exists in the JST object.
if (JST[path]) {
if(done) {
done(JST[path]);
}

return def.resolve(JST[path]);
}

// Fetch it asynchronously if not available from JST
$.get(path, function(contents) {
JST[path] = _.template(contents);

// Set the global JST cache and return the template
if(done) {
done(JST[path]);
}

// Resolve the template deferred
def.resolve(JST[path]);
}, "text");

// Ensure a normalized return value (Promise)
return def.promise();
},
```