Skip to content

Commit

Permalink
Snippets: added 'before' & 'after' callbacks
Browse files Browse the repository at this point in the history
Extension points for seamless manipulation with snippets before and after they are actualization. It can be used for unbinding event callbacks or registering new ones.
  • Loading branch information
vojtech-dobes committed Nov 29, 2012
1 parent 54ad29f commit dd59845
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
23 changes: 12 additions & 11 deletions extensions/scrollTo.ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
* Depends on 'snippets' extension
*/
$.nette.ext('scrollTo', {
success: function (payload) {
var snippetsExtension = this.ext('snippets', true);
if (payload.snippets) {
for (var id in payload.snippets) {
var $el = snippetsExtension.getElement(id);
if ($el.eq(0).tagName != 'TITLE') {
var offset = $el.offset();
scrollTo(offset.left, offset.top);
break;
}
init: function () {
this.ext('snippets', true).before($.proxy(function ($el) {
if (this.shouldTry && $el.eq(0).tagName != 'TITLE') {
var offset = $el.offset();
scrollTo(offset.left, offset.top);
this.shouldTry = false;
}
}
}), this);
},
success: function (payload) {
this.shouldTry = true;
}
}, {
shouldTry: true
});

})(jQuery);
39 changes: 31 additions & 8 deletions nette.ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,15 @@ $.nette.ext('validation', {
});

$.nette.ext('forms', {
success: function (payload) {
init: function () {
var snippets;
if (!window.Nette || !payload.snippets || !(snippets = this.ext('snippets'))) return;
if (!window.Nette || !(snippets = this.ext('snippets'))) return;

for (var id in payload.snippets) {
snippets.getElement(id).find('form').each(function() {
snippets.after(function ($el) {
$el.find('form').each(function() {
window.Nette.initForm(this);
});
}
});
},
prepare: function (settings) {
var analyze = settings.nette;
Expand Down Expand Up @@ -345,15 +345,38 @@ $.nette.ext('forms', {
// default snippet handler
$.nette.ext('snippets', {
success: function (payload) {
var snippets = [];
if (payload.snippets) {
for (var i in payload.snippets) {
this.updateSnippet(i, payload.snippets[i]);
var $el = this.getElement(i);
$.each(this.beforeQueue, function (index, callback) {
if (typeof callback == 'function') {
callback($el);
}
});
this.updateSnippet($el, payload.snippets[i]);
$.each(this.afterQueue, function (index, callback) {
if (typeof callback == 'function') {
callback($el);
}
});
}
}
this.before(snippets);
}
}, {
updateSnippet: function (id, html, back) {
var $el = this.getElement(id);
beforeQueue: [],
afterQueue: [],
before: function (callback) {
this.beforeQueue.push(callback);
},
after: function (callback) {
this.afterQueue.push(callback);
},
updateSnippet: function ($el, html, back) {
if (typeof $el == 'string') {
$el = this.getElement($el);
}
// Fix for setting document title in IE
if ($el.get(0).tagName == 'TITLE') {
document.title = html;
Expand Down

0 comments on commit dd59845

Please sign in to comment.