working on it ...

Filters

snippets
108k
followers
223
Published by snip2code

Javascript

This channel collects useful snippets for Javascript language
Sort by

Found 108k snippets

    public by toledano modified Mar 6, 2017  33  0  2  0

    gulpfile.js para cmi_tema

    gulpfile.js para cmi_tema: gulpfile.js
    /**
     * Created by toledano on 06/11/15.
     */
    
    // inicializa gulp
    var gulp = require('gulp');
    
    // incluye los plugin
    var jshint = require('gulp-jshint');
    var concat = require('gulp-concat');
    var debug = require('gulp-strip-debug');
    var uglify = require('gulp-uglify');
    var sass = require('gulp-ruby-sass');
    var notify = require('gulp-notify');
    
    // Configuración
    var config = {
      sourcesDir: "./sources",
      sassPath:   "./sources/sass",
      nodeDir:    "./node_modules",
    }
    
    // tarea fontawesome
    gulp.task('icons', function() {
      gulp.src([
        config.nodeDir + '/font-awesome/fonts/**.*',
        config.nodeDir + '/bootstrap-sass/assets/fonts/**/*'
      ])
        .pipe(gulp.dest('./assets/fonts'));
    });
    
    // tarea bootstrap
    gulp.task('sass', function() {
      sass(config.sassPath + '/cmi.scss', {
        style: 'expanded',   // nested, expanded, compact, compressed
        loadPath: [
          config.sassPath,
          config.nodeDir + '/bootstrap-sass/assets/stylesheets',
          config.nodeDir + '/font-awesome/scss'
        ]
      })
      .on("error", notify.onError(function (error) {
        return "Equivocación: " + error.message;
      }))
      .pipe(gulp.dest('./assets/css'));
    });
    
    // Se define la tarea `scripts`
    gulp.task('scripts', function() {
      gulp.src([
        config.nodeDir    + '/jquery/dist/jquery.js',
        config.nodeDir    + '/bootstrap-sass/assets/javascripts/bootstrap.js',
        config.sourcesDir + '/js/**/*',
      ])
      // .pipe(jshint())
      // .pipe(jshint.reporter('default'))
      .pipe(concat('cmi.js'))
      .pipe(debug())
      .pipe(uglify())
      .pipe(gulp.dest('./assets/js/'));
    });
    
    // la tarea watch
    gulp.task('watch', function() {
      gulp.watch(config.sassPath + '/**/*.scss', ['sass']);
      gulp.watch(config.sourcesDir + '/**/*.js', ['scripts'])
    });
    
    // la tarea `default`
    gulp.task('default', ['scripts','icons', 'sass']);
    
    
                

    public by toledano modified Mar 6, 2017  74  1  3  0

    Servicio para crear sesiones en el almacén local

    Servicio para crear sesiones en el almacén local: session.auth.service.js
    (function(angular){
      'use strict';
    
      angular
        .module('cmi')
        .service('session', sessionService);
    
      sessionService.$inject = ['$log', 'localStorage'];
    
      function sessionService($log, localStorage){
        // jshint validthis: true
        var self = this;
    
        // API Pública
        self.getUser = getUser;
        self.setUser = setUser;
        self.getAccessToken = getAccessToken;
        self.setAccessToken = setAccessToken;
        self.destroy = destroy;
    
        var _user = JSON.parse(localStorage.getItem('session.username'));
        var _accessToken = JSON.parse(localStorage.getItem('session.accessToken'));
    
        function getUser(){
          return _user;
        }     // getUser()
    
        function setUser(user){
          _user = user;
          localStorage.setItem('session.username', JSON.stringify(user));
          return _user;
        }     // setUser()
    
        function getAccessToken(){
          return _accessToken;
        }     // getAccessToken()
    
        function setAccessToken(token){
          _accessToken = token;
          localStorage.setItem('session.accessToken', JSON.stringify(token));
          return _accessToken;
        }     // setAccessToken()
    
        function destroy(){
          setUser(null);
          setAccessToken(null);
        }     // destroy()
      }
    })(angular);
    

    public by pleabargain modified Feb 19, 2017  34  1  2  0

    return current URL of the page

    //get current URL of page
    function myFunction() {
        let x = document.URL; 
        return x; 
      }
    //to execute
    myFunction();
    //returns something like
    //"https://www.snip2code.com/Edit/New"

    public by pleabargain modified Feb 19, 2017  129  6  5  2

    javascript output to console current full date as text

    // Enter here the actual content of the snippet.
    // Array of day names
    let dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
    				"Thursday","Friday","Saturday");
    
    // Array of month Names
    let monthNames = new Array(
    "January","February","March","April","May","June","July",
    "August","September","October","November","December");
    
    let now = new Date();
    console.log(dayNames[now.getDay()] + ", " +
    monthNames[now.getMonth()] +
    " " + now.getDate() +
    ", " + now.getFullYear());
    //output ~ Sunday, February 19, 2017
    

    public by pleabargain modified Feb 18, 2017  87  1  4  0

    add all function arguments together

    function sum(){
    	let result =0;
    	//loop through all of the values in func args
      for (i =0; i < arguments.length;i++){
        //now add them toghether
    		result += arguments[i];	
    	
    	}
    	return result;
    }
    sum(34,34,236,32);

    public by JavierSalazar modified Feb 14, 2017  132  5  3  0

    Pasar varios datos con ajax (Metodo POST)

    Pasar varios datos con ajax (Metodo POST)y es recibido por archivo php con la variable $_POST
    $.ajax(
    {
            url: "<?=resApi;?>procesarProducto.php",
            type: "post",
            dataType: "html",
            data: ({accion: "transferir",subaccion: "buscarStockSucursal", codigoprod: producto, sucursal: sucursal}),
            cache: false,
    })
        .done(function(res)
        {alert(res);}  

    public by waxmiguel modified Feb 1, 2017  98  0  4  1

    tortouise

    # TortoiseGit 
    
    $Global:TortoiseGitSettings = new-object PSObject -Property @{
      TortoiseGitPath = "C:\Program Files\TortoiseGit\bin\TortoiseProc.exe"
    }
    
    function tgit {
       if($args) {
        if($args[0] -eq "help") {
          # Replace the built-in help behaviour with just a list of commands
          $tortoiseGitCommands
          return    
        }
    
        $newArgs = @()
        $newArgs += "/command:" + $args[0]
        
        $cmd = $args[0]
        
        if($args.length -gt 1) {
          $args[1..$args.length] | % { $newArgs += $_ }
        }
          
        & $Global:TortoiseGitSettings.TortoiseGitPath $newArgs
      }
    }
    
    $tortoiseGitCommands = @(
    "about",
    "log",
    "commit",
    "add",
    "revert",
    "cleanup" ,
    "resolve",
    "switch",
    "export",
    "merge",
    "settings",
    "remove",
    "rename",
    "diff",
    "conflicteditor",
    "help",
    "ignore",
    "blame",
    "cat",
    "createpatch",
    "pull",
    "push",
    "rebase",
    "stashsave",
    "stashapply",
    "subadd",
    "subupdate",
    "subsync",
    "reflog",
    "refbrowse",
    "sync"
    ) | sort
    

    public by toledano modified Dec 8, 2016  39  0  1  0

    Un controlador con variable interna

    Un controlador con variable interna : calculadoraController.js
    function CalculadoraController(){
      var self = this;
      var _data = {
        resultado: 0
      };
    
      function Sumar(cantidad){
        _data.resultado += cantidad;
      }
    
      function Restar(cantidad){
        _data.resultado -= cantidad;
      }
    
      // API Pública
      self.contador = _data;
      self.suma = Sumar;
      self.resta = Restar;
    }
    
    
    

    public by toledano modified Dec 8, 2016  30  0  1  0

    Un controllador que no funciona por el alcance de las variables

    Un controllador que no funciona por el alcance de las variables: Calc2Controller.js
    function Calc2Controller(){
      var self = this;
      var _contador = 0;
    
      function _Suma(cantidad){
        _contador += cantidad;
      }
    
      function _Resta(cantidad){
        _contador -= cantidad;
      }
    
      self.suma = _Suma;
      self.resta = _Resta;
      self.contador = _contador;
    }
    
    
    

    public by toledano modified Dec 8, 2016  25  0  1  0

    Un control de angular. Espero que tenga las mejores prácticas.

    Un control de angular. Espero que tenga las mejores prácticas.: controller.js
    (function(angular){
      'use strict';
    
      RelojController.$inject = ['$scope', ];
      PrimerController.$inject = [];
    
      function RelojController($scope){
        // Variables internas
        var self = this;
        var _clock = {
          now: new Date()
        }
    
        // functiones internas
        function _updateClock(){
          _clock.now = new Date();
        }
    
        setInterval(function(){
          $scope.$apply(_updateClock);
        }, 1000);
    
        // API Pública
        self.clock = _clock;
      }
    
      angular.module('cmiApp')
        .controller('RelojController', RelojController);
    })(angular);
    
    
    • Public Snippets
    • Channels Snippets