Skip to content

Commit

Permalink
Fixed partial() tests to reflect the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
quirkey committed May 16, 2010
1 parent 0ad263a commit 4ea8bbc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 164 deletions.
113 changes: 31 additions & 82 deletions lib/sammy.js
Expand Up @@ -705,7 +705,7 @@
this.arounds.push(callback);
return this;
},

// Returns `true` if the current application is running.
isRunning: function() {
return this._running;
Expand Down Expand Up @@ -1200,7 +1200,7 @@
}

});

Sammy.RenderContext = function(event_context) {
this.event_context = event_context;
this.callbacks = [];
Expand All @@ -1209,9 +1209,9 @@
this.next_engine = false;
this.waiting = false;
};

$.extend(Sammy.RenderContext.prototype, {

load: function(location, options, callback) {
var context = this;
return this.then(function() {
Expand All @@ -1223,8 +1223,8 @@
options = $.extend({}, options);
}
if (callback) { this.then(callback); }
if (typeof location === 'string') {
// its a path
if (typeof location === 'string') {
// its a path
should_cache = !(options.cache === false);
delete options.cache;
if (options.engine) {
Expand All @@ -1233,7 +1233,7 @@
}
if (should_cache && (cached = this.event_context.app.templateCache(location))) {
return cached;
}
}
this.wait();
$.ajax($.extend({
url: location,
Expand Down Expand Up @@ -1265,15 +1265,15 @@
}
});
},

render: function(location, data, callback) {
if (_isFunction(location) && !data) {
return this.then(location);
} else {
return this.load(location).interpolate(data, location).then(callback);
}
},

collect: function(array, callback) {
var context = this;
return this.then(function() {
Expand All @@ -1286,7 +1286,7 @@
return contents;
});
},

renderEach: function(path, name, data, callback) {
if (_isArray(name)) {
callback = data;
Expand All @@ -1299,9 +1299,9 @@
return this.event_context.interpolate(this.content, idata, path);
});
},

then: function(callback) {
if (_isFunction(callback)) {
if (_isFunction(callback)) {
var context = this;
if (this.waiting) {
this.callbacks.push(callback);
Expand All @@ -1321,7 +1321,7 @@
}
return this;
},

interpolate: function(data, engine, retain) {
var context = this;
return this.then(function(content, prev) {
Expand All @@ -1333,39 +1333,39 @@
return retain ? prev + rendered : rendered;
});
},

swap: function() {
return this.then(function(content) {
this.event_context.swap(content);
});
},

appendTo: function(selector) {
return this.then(function(content) {
$(selector).append(content);
});
},

replace: function(selector) {
return this.then(function(content) {
$(selector).html(content);
});
},

wait: function() {
this.waiting = true;
},

next: function() {
this.waiting = false;
if (this.callbacks.length > 0) {
this.then(this.callbacks.shift());
}
}
}

});
// `Sammy.EventContext` objects are created every time a route is run or a

// `Sammy.EventContext` objects are created every time a route is run or a
// bound event is triggered. The callbacks for these events are evaluated within a `Sammy.EventContext`
// This within these callbacks the special methods of `EventContext` are available.
//
Expand Down Expand Up @@ -1410,7 +1410,7 @@
$element: function() {
return this.app.$element();
},

engineFor: function(engine) {
var context = this, engine_match;
// if path is actually an engine function just return it
Expand All @@ -1419,20 +1419,20 @@
engine = engine.toString();
if ((engine_match = engine.match(/\.([^\.]+)$/))) { engine = engine_match[1]; }
// set the engine to the default template engine if no match is found
if (engine && _isFunction(context[engine])) {
return context[engine];
if (engine && _isFunction(context[engine])) {
return context[engine];
}
if (context.app.template_engine) {
return context.app.template_engine;
return this.engineFor(context.app.template_engine);
}
return function(content, data) { return content; };
},

interpolate: function(content, data, engine) {
Sammy.log('engine for', engine, this.engineFor(engine));
return this.engineFor(engine).apply(this, [content, data]);
},

// Used for rendering remote templates or documents within the current application/DOM.
// By default Sammy and `partial()` know nothing about how your templates
// should be interpeted/rendered. This is easy to change, though. `partial()` looks
Expand Down Expand Up @@ -1481,60 +1481,9 @@
// rendered; //=> <li>I'm an item named Item 1</li> // for each element in the Array
// i; // the 0 based index of the itteration
// });
//
//
partial: function(path, data) {
return this.render(path, data).swap();
// var file_data,
// wrapped_callback,
// engine,
// data_array,
// cache_key = 'partial:' + path,
// context = this;
//
// // engine setup
// if ((engine = path.match(/\.([^\.]+)$/))) { engine = engine[1]; }
// // set the engine to the default template engine if no match is found
// if ((!engine || !$.isFunction(context[engine])) && this.app.template_engine) {
// engine = this.app.template_engine;
// }
// if (engine && !$.isFunction(engine) && $.isFunction(context[engine])) {
// engine = context[engine];
// }
// if (!callback && $.isFunction(data)) {
// // callback is in the data position
// callback = data;
// data = {};
// }
// data_array = ($.isArray(data) ? data : [data || {}]);
// wrapped_callback = function(response) {
// var new_content = response,
// all_content = "";
// $.each(data_array, function(i, idata) {
// if ($.isFunction(engine)) {
// new_content = engine.apply(context, [response, idata]);
// }
// // collect the content
// all_content += new_content;
// // if callback exists call it for each iteration
// if (callback) {
// // return the result of the callback
// // (you can bail the loop by returning false)
// return callback.apply(context, [new_content, i]);
// }
// });
// if (!callback) { context.swap(all_content); }
// context.trigger('changed');
// };
// if (this.app.cache_partials && this.cache(cache_key)) {
// // try to load the template from the cache
// wrapped_callback.apply(context, [this.cache(cache_key)]);
// } else {
// // the template wasnt cached, we need to fetch it
// $.get(path, function(response) {
// if (context.app.cache_partials) { context.cache(cache_key, response); }
// wrapped_callback.apply(context, [response]);
// });
// }
},

// Changes the location of the current window. If `to` begins with
Expand Down Expand Up @@ -1590,11 +1539,11 @@
toString: function() {
return "Sammy.EventContext: " + [this.verb, this.path, this.params].join(' ');
},

render: function(path, data, callback) {
return new Sammy.RenderContext(this).render(path, data, callback);
},

load: function(path, params, callback) {
return new Sammy.RenderContext(this).load(path, params, callback);
}
Expand Down

0 comments on commit 4ea8bbc

Please sign in to comment.