Skip to content

Instantly share code, notes, and snippets.

@satyapramodh
Forked from benstr/Meteor_uihooks.md
Created February 4, 2016 12:22
Show Gist options
  • Save satyapramodh/d52a5f9d1ae6a8400886 to your computer and use it in GitHub Desktop.
Save satyapramodh/d52a5f9d1ae6a8400886 to your computer and use it in GitHub Desktop.
_uihooks Examples
// from https://github.com/meteor/meteor/blob/devel/examples/localmarket/client/templates/app-body.js#L55-L110
Template.appBody.rendered = function() {
this.find("#content-container")._uihooks = {
insertElement: function(node, next) {
// short-circuit and just do it right away
if (initiator === 'menu')
return $(node).insertBefore(next);
var start = (initiator === 'back') ? '-100%' : '100%';
$.Velocity.hook(node, 'translateX', start);
$(node)
.insertBefore(next)
.velocity({translateX: [0, start]}, {
duration: ANIMATION_DURATION,
easing: 'ease-in-out',
queue: false
});
},
removeElement: function(node) {
if (initiator === 'menu')
return $(node).remove();
var end = (initiator === 'back') ? '100%' : '-100%';
$(node)
.velocity({translateX: end}, {
duration: ANIMATION_DURATION,
easing: 'ease-in-out',
queue: false,
complete: function() {
$(node).remove();
}
});
}
};
this.find(".notifications")._uihooks = {
insertElement: function(node, next) {
$(node)
.insertBefore(next)
.velocity("slideDown", {
duration: ANIMATION_DURATION,
easing: [0.175, 0.885, 0.335, 1.05]
});
},
removeElement: function(node) {
$(node)
.velocity("fadeOut", {
duration: ANIMATION_DURATION,
complete: function() {
$(node).remove();
}
});
}
};
}
// from https://github.com/meteor/meteor/blob/devel/examples/localmarket/client/templates/overlay.js#L27-L51
Template.overlay.rendered = function() {
this.find('#overlay-hook')._uihooks = {
insertElement: function(node, next, done) {
var $node = $(node);
$node
.hide()
.insertBefore(next)
.velocity('fadeIn', {
duration: ANIMATION_DURATION
});
},
removeElement: function(node, done) {
var $node = $(node);
$node
.velocity("fadeOut", {
duration: ANIMATION_DURATION,
complete: function() {
$node.remove();
}
});
}
}
}
// from https://github.com/xumx/magic-square/blob/fcf190c735a48a6c9413168a6141df89a844914f/client/transition-helper.js
var OFFSCREEN_CLASS = 'off-screen';
var EVENTS = 'webkitTransitionEnd oTransitionEnd transitionEnd msTransitionEnd transitionend';
var hooks = {
insertElement: function(node, next) {
$(node)
.addClass(OFFSCREEN_CLASS)
.insertBefore(next);
Deps.afterFlush(function() {
// call width to force the browser to draw it
$(node).width();
$(node).removeClass(OFFSCREEN_CLASS);
});
},
// we could do better I guess?
moveElement: function(node, next) {
hooks.removeElement(node);
hooks.insertElement(node, next);
},
removeElement: function(node) {
$(node).addClass(OFFSCREEN_CLASS)
.on(EVENTS, function() {
$(node).remove()
});
}
}
Template.transition.rendered = function() {
this.firstNode.parentNode._uihooks = hooks;
}

Add preliminary API for registering hooks to run when Blaze intends to insert, move, or remove DOM elements. For example, you can use these hooks to animate nodes as they are inserted, moved, or removed. To use them, you can set the _uihooks property on a container DOM element. _uihooks is an object that can have any subset of the following three properties:

  • insertElement: function (node, next): called when Blaze intends to insert the DOM element node before the element next
  • moveElement: function (node, next): called when Blaze intends to move the DOM element node before the element next
  • removeElement: function (node): called when Blaze intends to remove the DOM element node

Note that when you set one of these functions on a container element, Blaze will not do the actual operation; it's your responsibility to actually insert, move, or remove the node (by calling $(node).remove(), for example).

https://github.com/meteor/meteor/blob/30fb11f1fa0227f1c0ec3eb30b7864ea3b2d210e/History.md

// from https://github.com/gfk-ba/meteor-notifications/blob/master/notifications.js#L238-259
Template.notifications.rendered = function () {
this.firstNode._uihooks = {
insertElement: function (node, next) {
var settings = Notifications.settings;
$(node)
.addClass('notificationHidden')
.insertBefore(next)
.fadeIn({duration: settings.animationSpeed})
.promise()
.done(function () {
$(this).removeClass('notificationHidden');
});
},
removeElement: function (node) {
var settings = Notifications.settings;
$(node).animate(settings.hideAnimationProperties, {
duration: settings.animationSpeed,
complete: function () {
$(node).remove();
}});
}
};
};
// from https://github.com/meteor/redis-example/blob/ed39b420e26fc1a279438b86b0b23443591f5c3f/client/animations.js
Template.yodors.rendered = function () {
$('.debug')[0]._uihooks = {
insertElement: function (node, next) {
$('.debug li').addClass('shift-down');
$(node).addClass('pop-in').insertBefore(next);
setTimeout(function () {
$('.debug li').removeClass('shift-down');
$(node).removeClass('pop-in');
}, 230);
}
};
};
// from https://github.com/meteor/meteor/blob/c78e04645d970b455c0ef405889242ae9b8e6c47/examples/todos/client/templates/app-body.js#L35-L51
Template.appBody.rendered = function() {
this.find('#content-container')._uihooks = {
insertElement: function(node, next) {
$(node)
.hide()
.insertBefore(next)
.fadeIn(function () {
listFadeInHold.release();
});
},
removeElement: function(node) {
$(node).fadeOut(function() {
$(this).remove();
});
}
};
};
// from https://github.com/meteor/meteor/blob/c78e04645d970b455c0ef405889242ae9b8e6c47/examples/todos/client/templates/lists-show.js#L9-L33
Template.listsShow.rendered = function() {
if (firstRender) {
// Released in app-body.js
listFadeInHold = LaunchScreen.hold();
// Handle for launch screen defined in app-body.js
listRenderHold.release();
firstRender = false;
}
this.find('.js-title-nav')._uihooks = {
insertElement: function(node, next) {
$(node)
.hide()
.insertBefore(next)
.fadeIn();
},
removeElement: function(node) {
$(node).fadeOut(function() {
this.remove();
});
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment