Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed partial() tests to reflect the new API

  • Loading branch information...
commit 4ea8bbc8382f01654bb25eff25a9361dba207545 1 parent 0ad263a
@quirkey quirkey authored
Showing with 47 additions and 164 deletions.
  1. +31 −82 lib/sammy.js
  2. +16 −82 test/test_sammy_event_context.js
View
113 lib/sammy.js
@@ -705,7 +705,7 @@
this.arounds.push(callback);
return this;
},
-
+
// Returns `true` if the current application is running.
isRunning: function() {
return this._running;
@@ -1200,7 +1200,7 @@
}
});
-
+
Sammy.RenderContext = function(event_context) {
this.event_context = event_context;
this.callbacks = [];
@@ -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() {
@@ -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) {
@@ -1233,7 +1233,7 @@
}
if (should_cache && (cached = this.event_context.app.templateCache(location))) {
return cached;
- }
+ }
this.wait();
$.ajax($.extend({
url: location,
@@ -1265,7 +1265,7 @@
}
});
},
-
+
render: function(location, data, callback) {
if (_isFunction(location) && !data) {
return this.then(location);
@@ -1273,7 +1273,7 @@
return this.load(location).interpolate(data, location).then(callback);
}
},
-
+
collect: function(array, callback) {
var context = this;
return this.then(function() {
@@ -1286,7 +1286,7 @@
return contents;
});
},
-
+
renderEach: function(path, name, data, callback) {
if (_isArray(name)) {
callback = data;
@@ -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);
@@ -1321,7 +1321,7 @@
}
return this;
},
-
+
interpolate: function(data, engine, retain) {
var context = this;
return this.then(function(content, prev) {
@@ -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.
//
@@ -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
@@ -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
@@ -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
@@ -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);
}
View
98 test/test_sammy_event_context.js
@@ -1,6 +1,6 @@
(function($) {
with(QUnit) {
-
+
var test_app = new Sammy.Application(function() {
this.raise_errors = false;
this.element_selector = '#main';
@@ -25,7 +25,7 @@
.should('set params', function() {
deepEqual(this.context.params, new Sammy.Object({test: 'hooray'}));
});
-
+
context('Sammy', 'EventContext', 'redirect', {
before: function() {
@@ -63,7 +63,7 @@
context.notFound();
});
});
-
+
context('Sammy', 'EventContext', 'partial', {
before: function() {
this.app = test_app;
@@ -73,15 +73,15 @@
})
.should('pass contents to callback', function() {
var contents = '';
- this.context.partial('fixtures/partial.html', function(data) { contents = data; });
+ this.context.partial('fixtures/partial.html').then(function(data) { contents = data; });
soon(function () {
equal(contents, '<div class="test_partial">PARTIAL</div>');
}, this, 2);
})
.should('not run through template() if Sammy.Template is not present', function() {
var contents = '';
- this.context.partial('fixtures/partial.template', {name: 'TEMPLATE!', class_name: 'test_template'}, function(data) {
- contents = data;
+ this.context.partial('fixtures/partial.template', {name: 'TEMPLATE!', class_name: 'test_template'}).then(function(data) {
+ contents = data;
});
soon(function () {
equal(contents, '<div class="<%= class_name %>"><%= name %></div>');
@@ -92,65 +92,13 @@
var app = new Sammy.Application(function() { this.element_selector = '#main'; });
app.use(Sammy.Template);
this.context = new app.context_prototype(app);
- this.context.partial('fixtures/partial.template', {name: 'TEMPLATE!', class_name: 'test_template'}, function(data) {
- contents = data;
+ this.context.partial('fixtures/partial.template', {name: 'TEMPLATE!', class_name: 'test_template'}).then(function(data) {
+ contents = data;
});
soon(function () {
equal(contents, '<div class="test_template">TEMPLATE!</div>');
}, this, 2);
})
- .should('itterate over data if data is an array', function() {
- var contents = '',
- app = new Sammy.Application(function() { this.element_selector = '#main'; }),
- data = [{name: 'one', class_name: 'it-1'}, {name: 'two', class_name: 'it-2'}],
- expected = '<div class="it-1">one</div><div class="it-2">two</div>';
-
- app.use(Sammy.Template);
- this.context = new app.context_prototype(app);
- this.context.partial('fixtures/partial.template', data);
- this.context.partial('fixtures/partial.template', data, function(html) {
- contents += html;
- });
- soon(function () {
- equal($('#main').html(), expected);
- equal(contents, expected);
- }, this, 2, 2);
- })
- .should('cache template if cache() is present', function(){
- var contents = '';
- var app = new Sammy.Application(function() { this.element_selector = '#main'; });
- app.use(Sammy.Template);
- app.use(Sammy.Cache);
- app.clearCache();
- this.context = new app.context_prototype(app);
- this.context.partial('fixtures/partial.html', function(data) {
- contents = data;
- });
- soon(function () {
- equal(contents, '<div class="test_partial">PARTIAL</div>');
- equal(app.cache('partial:fixtures/partial.html'), '<div class="test_partial">PARTIAL</div>');
- this.context.partial('fixtures/partial.html', function(data) {
- contents = data;
- });
- equal(contents, '<div class="test_partial">PARTIAL</div>');
- }, this, 1, 3);
- })
- .should('not cache template if cache is present and cache_partials: false', function() {
- var contents = '';
- var app = new Sammy.Application(function() { this.element_selector = '#main'; });
- app.use(Sammy.Template);
- app.use(Sammy.Cache);
- app.clearCache();
- app.cache_partials = false;
- this.context = new app.context_prototype(app);
- this.context.partial('fixtures/partial.html', function(data) {
- contents = data;
- });
- soon(function () {
- equal(contents, '<div class="test_partial">PARTIAL</div>');
- ok(!app.cache('partial:fixtures/partial.html'));
- }, this, 1, 2);
- })
.should('replace default app element if no callback is passed', function() {
var contents = '';
var app = new Sammy.Application(function() { this.element_selector = '#main'; });
@@ -162,26 +110,12 @@
equal(app.$element().children('.test_template').length, 1);
}, this, 2, 2);
})
- .should('trigger changed after the partial callback', function() {
- var changed = false;
- test_app.bind('changed', function() {
- changed = true;
- });
- test_app.run();
- this.context.partial('fixtures/partial.html', function(data) {
- changed = false;
- });
- soon(function() {
- ok(changed);
- test_app.unload();
- });
- })
.should('use default engine if provided and template doesnt match an engine', function() {
var contents = '';
- var app = new Sammy.Application(function() {
- this.element_selector = '#main';
+ var app = new Sammy.Application(function() {
+ this.element_selector = '#main';
this.template_engine = 'template';
-
+
this.helper('template', function(template, data) {
return "!!!" + template.toString() + "!!!";
});
@@ -194,8 +128,8 @@
})
.should('use default engine as a method if template doesnt match an engine', function() {
var contents = '';
- var app = new Sammy.Application(function() {
- this.element_selector = '#main';
+ var app = new Sammy.Application(function() {
+ this.element_selector = '#main';
this.template_engine = function(template, data) {
return "!!!" + template.toString() + "!!!";
};
@@ -206,7 +140,7 @@
equal(app.$element().text(), '!!!NOENGINE!!!');
});
});
-
+
context('Sammy', 'EventContext', 'trigger', {
before: function() {
this.context = test_context;
@@ -249,6 +183,6 @@
deepEqual(passed_data, test_data);
});
});
-
+
};
-})(jQuery);
+})(jQuery);
Please sign in to comment.
Something went wrong with that request. Please try again.