Permalink
Browse files

partials support for the partial method and complete support for the …

…render method
  • Loading branch information...
1 parent 405c98d commit f32bfe22c28161e75f572e9b443faff34df7d57e @endor endor committed Jul 9, 2012
View
@@ -1585,6 +1585,17 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
// `load()` a template and then `interpolate()` it with data.
//
+ // can be called with multiple different signatures:
+ //
+ // this.render(callback);
+ // this.render('/location');
+ // this.render('/location', {some: data});
+ // this.render('/location', callback);
+ // this.render('/location', {some: data}, callback);
+ // this.render('/location', {some: data}, {my: partials});
+ // this.render('/location', callback, {my: partials});
+ // this.render('/location', {some: data}, callback, {my: partials});
+ //
// ### Example
//
// this.get('#/', function() {
@@ -1593,8 +1604,20 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
//
render: function(location, data, callback, partials) {
if (_isFunction(location) && !data) {
+ // invoked as render(callback)
return this.then(location);
} else {
+ if(_isFunction(data)) {
+ // invoked as render(location, callback, [partials])
+ partials = callback;
+ callback = data;
+ data = null;
+ } else if(callback && !_isFunction(callback)) {
+ // invoked as render(location, data, partials)
+ partials = callback;
+ callback = null;
+ }
+
return this.loadPartials(partials)
.load(location)
.interpolate(data, location)
@@ -1604,14 +1627,16 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
// `render()` the `location` with `data` and then `swap()` the
// app's `$element` with the rendered content.
- partial: function(location, data, callback) {
+ partial: function(location, data, callback, partials) {
if (_isFunction(callback)) {
- return this.render(location, data).swap(callback);
- } else if (!callback && _isFunction(data)) {
- // invoked as partial(location, callback)
- return this.render(location).swap(data);
+ // invoked as partial(location, data, callback, [partials])
+ return this.render(location, data, partials).swap(callback);
+ } else if (_isFunction(data)) {
+ // invoked as partial(location, callback, [partials])
+ return this.render(location, {}, callback).swap(data);
} else {
- return this.render(location, data).swap();
+ // invoked as partial(location, data, [partials])
+ return this.render(location, data, callback).swap();
}
},
@@ -1885,8 +1910,8 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
// `render()` the `location` with `data` and then `swap()` the
// app's `$element` with the rendered content.
- partial: function(location, data, callback) {
- return new Sammy.RenderContext(this).partial(location, data, callback);
+ partial: function(location, data, callback, partials) {
+ return new Sammy.RenderContext(this).partial(location, data, callback, partials);
},
// create a new `Sammy.RenderContext` calling `send()` with an arbitrary
@@ -0,0 +1 @@
+<span>my name</span>
@@ -0,0 +1 @@
+<div class="blah">{{>item}}</div>
@@ -80,7 +80,7 @@
this.context.partial('fixtures/partial.html').then(function(data) { contents = data; });
soon(function () {
sameHTML(contents, '<div class="test_partial">PARTIAL</div>');
- }, this, 2);
+ }, this, 4);
})
.should('not run through template() if Sammy.Template is not present', function() {
var contents = '';
@@ -115,18 +115,6 @@
sameHTML(contents, '<div class="test_template">TEMPLATE!</div>');
}, this, 2);
})
- .should('allow rendering partials', function() {
- var contents = '';
- var app = new Sammy.Application(function() { this.element_selector = '#main'; });
- app.use(Sammy.Mustache);
- this.context = new app.context_prototype(app);
- this.context.render('fixtures/partial.mustache', {name: 'TEMPLATE!', class_name: 'test_template'}, function(data) {
- contents = data;
- }, {item: 'fixtures/item.mustache'});
- soon(function () {
- sameHTML(contents, '<div class="test_template"><span>TEMPLATE!</span></div>');
- }, this, 2);
- })
.should('replace default app element if no callback is passed', function() {
var contents = '';
var app = new Sammy.Application(function() { this.element_selector = '#main'; });
@@ -167,6 +155,47 @@
soon(function () {
equal(app.$element().text(), '!!!NOENGINE!!!');
});
+ })
+ .should('render partials with callback and data', function() {
+ var contents = '';
+ var app = new Sammy.Application(function() {
+ this.element_selector = '#main';
+ this.use(Sammy.Mustache);
+ });
+ this.context = new app.context_prototype(app);
+ this.context.partial('fixtures/partial.mustache', {name: 'TEMPLATE!', class_name: 'test_template'}, function(data) {
+ contents = data;
+ }, {item: 'fixtures/item.mustache'});
+ soon(function () {
+ sameHTML(contents, '<div class="test_template"><span>TEMPLATE!</span></div>');
+ sameHTML(app.$element().html(), '<div class="test_template"><span>TEMPLATE!</span></div>');
+ }, this, 2, 2);
+ })
+ .should('render partials without callback but with data', function() {
+ var app = new Sammy.Application(function() {
+ this.element_selector = '#main';
+ this.use(Sammy.Mustache);
+ });
+ this.context = new app.context_prototype(app);
+ this.context.partial('fixtures/partial.mustache', {name: 'TEMPLATE!', class_name: 'test_template'}, {item: 'fixtures/item.mustache'});
+ soon(function () {
+ sameHTML(app.$element().html(), '<div class="test_template"><span>TEMPLATE!</span></div>');
+ }, this, 2);
+ })
+ .should('render partials without data but with callback', function() {
+ var contents = '';
+ var app = new Sammy.Application(function() {
+ this.element_selector = '#main';
+ this.use(Sammy.Mustache);
+ });
+ this.context = new app.context_prototype(app);
+ this.context.partial('fixtures/partial2.mustache', function(data) {
+ contents = data;
+ }, {item: 'fixtures/item2.mustache'});
+ soon(function () {
+ sameHTML(contents, '<div class="blah"><span>my name</span></div>');
+ sameHTML(app.$element().html(), '<div class="blah"><span>my name</span></div>');
+ }, this, 2, 2);
});
context('Sammy', 'EventContext', 'trigger', {
@@ -16,6 +16,7 @@
this.raise_errors = false;
this.element_selector = '#main';
this.use(Sammy.Template);
+ this.use(Sammy.Mustache);
});
var test_context = new test_app.context_prototype(test_app, 'get', '#/test/:test', {test: 'hooray'});
this.runRouteAndAssert = function(callback, test_callback, expects) {
@@ -292,6 +293,36 @@
sameHTML($('#test_area').html(), '<div class="class-name">name<span>other name</span></div>');
});
})
+ .should('render with partials and callback and data', function() {
+ var callback = function(context) {
+ this.render('fixtures/partial.mustache', {name: 'my name', class_name: 'class'}, function(data) {
+ $('#test_area').html(data);
+ }, {item: 'fixtures/item.mustache'});
+ };
+ this.runRouteAndAssert(callback, function() {
+ sameHTML($('#test_area').html(), '<div class="class"><span>my name</span></div>', "render contents");
+ });
+ })
+ .should('render with partials without callback but with data', function() {
+ $('#test_area').html('');
+ var callback = function(context) {
+ this.render('fixtures/partial.mustache', {name: 'my name', class_name: 'class'}, {item: 'fixtures/item.mustache'})
+ .appendTo('#test_area');
+ };
+ this.runRouteAndAssert(callback, function() {
+ sameHTML($('#test_area').html(), '<div class="class"><span>my name</span></div>', "render contents");
+ });
+ })
+ .should('render with partials without data but with callback', function() {
+ var callback = function(context) {
+ this.render('fixtures/partial2.mustache', function(data) {
+ $('#test_area').html(data);
+ }, {item: 'fixtures/item2.mustache'});
+ };
+ this.runRouteAndAssert(callback, function() {
+ sameHTML($('#test_area').html(), '<div class="blah"><span>my name</span></div>', "render contents");
+ });
+ })
.should('iterate and collect with each', function() {
var callback = function(context) {
this.load('fixtures/list.html').replace('#test_area')

0 comments on commit f32bfe2

Please sign in to comment.