Skip to content

Commit dd59845

Browse files
committed
Snippets: added 'before' & 'after' callbacks
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.
1 parent 54ad29f commit dd59845

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

extensions/scrollTo.ajax.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
* Depends on 'snippets' extension
55
*/
66
$.nette.ext('scrollTo', {
7-
success: function (payload) {
8-
var snippetsExtension = this.ext('snippets', true);
9-
if (payload.snippets) {
10-
for (var id in payload.snippets) {
11-
var $el = snippetsExtension.getElement(id);
12-
if ($el.eq(0).tagName != 'TITLE') {
13-
var offset = $el.offset();
14-
scrollTo(offset.left, offset.top);
15-
break;
16-
}
7+
init: function () {
8+
this.ext('snippets', true).before($.proxy(function ($el) {
9+
if (this.shouldTry && $el.eq(0).tagName != 'TITLE') {
10+
var offset = $el.offset();
11+
scrollTo(offset.left, offset.top);
12+
this.shouldTry = false;
1713
}
18-
}
14+
}), this);
15+
},
16+
success: function (payload) {
17+
this.shouldTry = true;
1918
}
19+
}, {
20+
shouldTry: true
2021
});
2122

2223
})(jQuery);

nette.ajax.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,15 @@ $.nette.ext('validation', {
302302
});
303303

304304
$.nette.ext('forms', {
305-
success: function (payload) {
305+
init: function () {
306306
var snippets;
307-
if (!window.Nette || !payload.snippets || !(snippets = this.ext('snippets'))) return;
307+
if (!window.Nette || !(snippets = this.ext('snippets'))) return;
308308

309-
for (var id in payload.snippets) {
310-
snippets.getElement(id).find('form').each(function() {
309+
snippets.after(function ($el) {
310+
$el.find('form').each(function() {
311311
window.Nette.initForm(this);
312312
});
313-
}
313+
});
314314
},
315315
prepare: function (settings) {
316316
var analyze = settings.nette;
@@ -345,15 +345,38 @@ $.nette.ext('forms', {
345345
// default snippet handler
346346
$.nette.ext('snippets', {
347347
success: function (payload) {
348+
var snippets = [];
348349
if (payload.snippets) {
349350
for (var i in payload.snippets) {
350-
this.updateSnippet(i, payload.snippets[i]);
351+
var $el = this.getElement(i);
352+
$.each(this.beforeQueue, function (index, callback) {
353+
if (typeof callback == 'function') {
354+
callback($el);
355+
}
356+
});
357+
this.updateSnippet($el, payload.snippets[i]);
358+
$.each(this.afterQueue, function (index, callback) {
359+
if (typeof callback == 'function') {
360+
callback($el);
361+
}
362+
});
351363
}
352364
}
365+
this.before(snippets);
353366
}
354367
}, {
355-
updateSnippet: function (id, html, back) {
356-
var $el = this.getElement(id);
368+
beforeQueue: [],
369+
afterQueue: [],
370+
before: function (callback) {
371+
this.beforeQueue.push(callback);
372+
},
373+
after: function (callback) {
374+
this.afterQueue.push(callback);
375+
},
376+
updateSnippet: function ($el, html, back) {
377+
if (typeof $el == 'string') {
378+
$el = this.getElement($el);
379+
}
357380
// Fix for setting document title in IE
358381
if ($el.get(0).tagName == 'TITLE') {
359382
document.title = html;

0 commit comments

Comments
 (0)