Skip to content

Instantly share code, notes, and snippets.

@zefei
zefei / vimfiler-settings.vim
Created October 17, 2014 00:54
vimfiler settings for better toggle behavior
" use this function to toggle vimfiler
function! s:vimfiler_toggle()
if &filetype == 'vimfiler'
execute 'silent! buffer #'
if &filetype == 'vimfiler'
execute 'enew'
endif
elseif exists('t:vimfiler_buffer') && bufexists(t:vimfiler_buffer)
execute 'buffer ' . t:vimfiler_buffer
else
@zefei
zefei / angular-meteor-update-collection.js
Last active August 29, 2015 14:07
Using Angular and Meteor together: improved collection updates
// compare newArray and oldArray, copy unchanged oldArray items over to
// newArray, hence bypass Angular dirty check for these items
function updateCollection(newArray, oldArray) {
if (!newArray || !oldArray) return newArray;
for (var i = 0; i < newArray.length; i++) {
for (var j = 0; j < oldArray.length; j++) {
if (angular.equals(newArray[i], oldArray[j])) {
newArray[i] = oldArray[j];
break;
@zefei
zefei / angular-meteor-autorun-example.js
Created September 1, 2014 02:42
Using Angular and Meteor together: use autorun wrapper
// inside your controller
autorun($scope, function() {
Meteor.subscribe('myMeteorCollection');
$scope.myModel = MyMeteorCollection.find().fetch();
});
@zefei
zefei / angular-meteor-delimiter.js
Created September 1, 2014 02:39
Using Angular and Meteor together: template delimiter
// change Angular's template delimiter to [[...]]
angular.module('myApp')
.config(['$interpolateProvider',
function($interpolateProvider) {
$interpolateProvider.startSymbol('[[').endSymbol(']]');
}
]);
@zefei
zefei / angular-meteor-templates.js
Created September 1, 2014 02:38
Using Angular and Meteor together: templates (post Meteor 0.8)
// for Meteor >= 0.8.0
// load Template.* into $templateCache
angular.module('myApp')
.run(['$templateCache', function($templateCache) {
angular.forEach(Template, function(template, name) {
// Meteor uses Template to store its own templates, too
// Your template file shouldn't start with _ to avoid conflict
if (name[0] !== '_' && name !== 'prototype') {
var node = document.createElement('div');
@zefei
zefei / angular-meteor-templates-pre0.8.js
Created September 1, 2014 02:37
Using Angular and Meteor together: templates (pre Meteor 0.8)
// for Meteor < 0.8.0
// load Template.* into $templateCache
angular.module('myApp')
.run(['$templateCache', function($templateCache) {
angular.forEach(Template, function(render, name) {
// Meteor uses Template to store its own templates, too
// Your template file shouldn't start with _ to avoid conflict
if (name[0] !== '_') $templateCache.put(name, render());
@zefei
zefei / angular-meteor-example3.js
Created September 1, 2014 02:32
Example of using Meteor reactivity inside Angular: using autorun with $scope.$watch
// a reactive block inside a $watch block
// in this case, you have to manually stop previous autorun
// inside $watch block
var myAutorun;
$scope.$watch('priceTotal', function() {
if (myAutorun) myAutorun.stop()
myAutorun = $autorun($scope, function() {
var shipping = Session.get('shipping');
$scope.total = $scope.priceTotal + shipping;
});
@zefei
zefei / angular-meteor-example2.js
Last active August 29, 2015 14:05
Example of using Meteor reactivity inside Angular: using Meteor sessions
// a reactive block that depends on a collection (Products)
// and a session value
autorun($scope, function() {
Meteor.subscribe('products');
var myCity = Session.get('myCity');
$scope.products = updateCollection(Products.find({city: myCity}).fetch(), $scope.products);
});
...
// any of following lines will trigger the reactive block above
@zefei
zefei / angular-meteor-example1.js
Created September 1, 2014 02:28
Example of using Meteor reactivity inside Angular: using Meteor accounts
// a reactive block that depends on user account
autorun($scope, function() {
$scope.user = Meteor.user();
$scope.loggedIn = !!user;
});
...
// any of following lines will trigger the reactive block above
Meteor.loginWithPassword(myEmail, myPassword);
Accounts.createUser({email: myEmail, password: myPassword);
@zefei
zefei / angular-meteor-autorun.js
Last active August 29, 2015 14:05
Using Angular and Meteor together: reactive wrapper
// bind a Meteor reactive computation to the current scope
angular.module('myApp')
.factory('autorun', function() {
return function(scope, fn) {
// wrapping around Deps.autorun
var comp = Deps.autorun(function(c) {
fn(c);
// this is run immediately for the first call
// but after that, we need to $apply to start Angular digest