Last active
August 29, 2015 14:02
-
-
Save hamxiaoz/ae3ca37bd30fe89c5245 to your computer and use it in GitHub Desktop.
Helpers or Utilities I used in Meteor.js project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# _.mixin | |
# "eachSlice": (obj, size, iterator, context)-> | |
# for (i=0, l=obj.length; i < l; i+=size) | |
# iterator.call(context, obj.slice(i,i+size), i, obj) | |
# http://stackoverflow.com/questions/10249658 | |
` | |
Array.prototype.eachSlice = function (size, callback){ | |
for (var i = 0, l = this.length; i < l; i += size){ | |
callback.call(this, this.slice(i, i + size)); | |
} | |
}; | |
` | |
# string.startsWith and endsWith | |
# http://stackoverflow.com/a/646643/166286 | |
# http://stackoverflow.com/a/2548133 | |
` | |
if (typeof String.prototype.startsWith != 'function') { | |
String.prototype.startsWith = function (str){ | |
return this.slice(0, str.length) == str; | |
}; | |
} | |
if (typeof String.prototype.endsWith != 'function') { | |
String.prototype.endsWith = function (str){ | |
return this.slice(-str.length) == str; | |
}; | |
} | |
` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UI.registerHelper 'sessionGet', (input, subKey)-> | |
obj = Session.get(input) | |
if obj? | |
return obj[subKey] | |
else | |
return obj | |
UI.registerHelper 'dateStr', (date)-> | |
return moment(date).format('L') | |
UI.registerHelper 'dateStrFull', (date)-> | |
return moment(date).format('MM/DD/YYYY LT') | |
UI.registerHelper 'acronymEmail', (email)-> | |
if email? | |
return email.substring(0,2).toUpperCase() | |
else | |
return '' | |
UI.registerHelper 'rev', -> | |
return 'rev ' + Meteor.settings.public.rev | |
# Utility | |
UI.registerHelper 'humanizeBoolean', (booleanValue)-> | |
if booleanValue | |
return 'Yes' | |
else | |
return 'No' | |
UI.registerHelper 'activeIf', (str1, str2)-> | |
if str1 == str2 then return 'active' else return '' | |
# HACK you cannot write array in template and use it in helper | |
# http://stackoverflow.com/questions/16783463/pass-array-written-in-template-to-meteor-handlebars-helper | |
# doens't work: | |
# li(class="{{activeIf CurrentPathName ['tools', 'directDownload', 'essUsage'] }}") | |
UI.registerHelper 'activeIfStarts', (str1, str2)-> | |
if str1.startsWith str2 then return 'active' else return '' | |
UI.registerHelper 'ifThen', (cp1, cp2, strWhenTrue, strWhenFalse)-> | |
if cp1 == cp2 then return strWhenTrue else return strWhenFalse | |
UI.registerHelper 'idfy', (str)-> | |
return idEncode(str, '') | |
UI.registerHelper 'idfy', (str, str2, str3)-> | |
return idEncode(str+str2+str3, '') | |
# get path name: 'debug', instead of '/debug' | |
UI.registerHelper 'currentPathName', -> | |
current = Router.current() | |
if current? then return current.route.name else return '' | |
# get the path: '/a/bc' | |
UI.registerHelper 'currentPath', -> | |
current = Router.current() | |
if current? then return current.path else return '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/hharnisc/meteor-accounts-admin-ui-bootstrap-3/blob/bc613e55d4e8c9b419b798e92d274dc697d68b15/client/accounts_admin.js#L35 | |
// search no more than 2 times per second | |
var setUserFilter = _.throttle(function(template) { | |
var search = template.find(".search-input-filter").value; | |
Session.set("userFilter", search); | |
}, 500); | |
Template.accountsAdmin.events({ | |
'keyup .search-input-filter': function(event, template) { | |
setUserFilter(template); | |
return false; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment