Skip to content

Instantly share code, notes, and snippets.

@martianyi
martianyi / Gruntfile.js
Last active March 21, 2017 02:18
Gruntfile.js
module.exports = function (grunt) {
// Load grunt tasks automatically
require('load-grunt-tasks')(grunt);
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
grunt.initConfig({
@martianyi
martianyi / classRoute.js
Last active March 21, 2017 02:24
angularjs classRoute directive, usage <body class-route=" ">
var classRoute = angular.module('directives.classRoute',[]);
classRoute.directive('classRoute', function($rootScope, $route) {
return function(scope, elem, attr) {
var previous = '';
$rootScope.$on('$routeChangeSuccess', function(event, currentRoute) {
var route = currentRoute.$$route;
if(route) {
@martianyi
martianyi / fileModel.js
Last active March 21, 2017 02:16
angularjs directive to upload file
var fileModel = angular.module('directives.fileModel', []);
fileModel.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
@martianyi
martianyi / securityInterceptor.js
Last active March 21, 2017 02:23
angularjs security interceptor
var securityInterceptor = angular.module('security.interceptor', []);
//securityInterceptor, add token to page_header
securityInterceptor.factory('securityInterceptor', ['$q', '$cookieStore', '$location', 'toaster',
function ($q, $cookieStore, $location, toaster) {
var token = $cookieStore.get('token');
return {
'request': function (config) {
config.headers = config.headers || {};
if (token) {
@martianyi
martianyi / securityService.js
Last active March 21, 2017 02:23
angularjs security service
var securityService = angular.module('security.service', []);
//Security
securityService.factory('Security', [
'$http', '$location', '$cookieStore', 'API_SERVER', '$window', '$route', 'toaster',
function ($http, $location, $cookieStore, API_SERVER, $window, $route, toaster) {
var security = {};
//login
@martianyi
martianyi / rangeFilter.js
Created May 7, 2015 04:20
range filter using with angular rangeslider
app.filter('rangeFilter', function () {
return function (items, sliderRanges) {
var filtered = [];
var expectedReturnMin = sliderRanges.expectedReturnMin;
var expectedReturnMax = sliderRanges.expectedReturnMax;
var durationMin = sliderRanges.durationMin;
var durationMax = sliderRanges.durationMax;
angular.forEach(items, function (item) {
@martianyi
martianyi / routeData.js
Last active March 21, 2017 02:22
angularjs service to update settings when route change
var routeData = angular.module('RouteData', []);
routeData.provider('RouteData', function () {
var settings = {};
var hookToRootScope = false;
this.applyConfig = function (newSettings) {
settings = newSettings;
};
@martianyi
martianyi / head.html
Last active May 4, 2020 16:23
meta prevent wechat browser from caching. via https://www.v2ex.com/t/140680
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
@martianyi
martianyi / loader.js
Last active March 21, 2017 02:21
simple loader
var flag = false;
var timer = null;
window.addEventListener('load', function () {
if (!flag) {
flag = true;
document.getElementById("load").style["display"] = "none";
clearTimeout(timer);
}
}, false);
timer = setTimeout(function () {
@martianyi
martianyi / sharedProperties.js
Last active August 2, 2017 01:48
angularjs service to share properties between functions
//share properties between functions
angular
.module('app', [])
.service('sharedProperties', function () {
var hashtable = {};
return {
setValue: function (key, value) {
hashtable[key] = value;
},