Saturday, February 16, 2013

Esquema para crear objetos instanciable para javascript con jQuery

Con este esquema puedes crear objetos instanciables en Javascript usando jQuery.
Su uso es muy simple, sólo debes copiar el siguiente código y cambiar las variables que están en mayúsculas.

El esquema

// Helper. Sólo usarlo una vez. Esto resuelve varios problemas con objetos en js.
if ( typeof Object.create !== 'function' ) {
    Object.create = function( obj ) {
        function F() {};
        F.prototype = obj;
        return new F();
    };
}

// Esquema para objetos javascript.
(function(){
    var OBJ_NAME = {
        props: {},
        init: function(props){
            this.props = $.extend({}, this.props, props);
            return this;
        }
    };

    // Para instanciar y ejecutar constructor.
    new_OBJ_NAME = function(props){
        var REL_OBJ_NAME = Object.create(OBJ_NAME);
        return REL_OBJ_NAME.init(props);
    };
})();

// Uso.
props = {/* Las propiedades*/};
obj = new_OBJ_NAME(props);

Ejemplo

(function(){
    var mi_objeto = {
        props: {prop1 : 1, prop2 : 2},

        init: function(props){
            this.props = $.extend({}, this.props, props);
            return this;
        },

        sumar: function(){
            return this.props.prop1 + this.props.prop2;
        }
    };
    new_mi_objeto = function(props){
        var mi_objeto_instanciado = Object.create(mi_objeto);
        return mi_objeto_instanciado.init(props);
    };
})();

// Uso.
// Usamos propiedades por defecto.
obj1 = new_mi_objeto();
console.log(obj1.sumar());

// Usamos propiedades personalizadas.
props = {prop1 : 1234, prop2 : 8876};
obj2 = new_mi_objeto(props);
console.log(obj2.sumar());

Créditos

Tuesday, August 28, 2012

Xubuntu: Monitor dual con VGA primario

Gracias a este post pude dar con la solución al problema que tenía casi desde que utilizo Ubunutu:

Usar dos monitores y elegir cuál es el primario

Pero el post no me daba con la solución. Seguí indagando en la configuración y realicé algunos cambios y al fin pude dejar Mi monitor VGA como primario y el de mi Notebook como secundario:
  1. Ir a Configuración > Editor de configuración
  2. Fijarse que se esté en el canal 'Displays'
  3. Desplegar propiedad 'Default
    1. Debiesen aparecer 2 sub-propiedades. Mi caso: LVDS1 (Notebook) y VGA1 (Monitor externo)
  4. Tomar el valor de X del monitor que dejarás como principal (VGA1)
    1. VGA1 > Resolution. Mi caso: 1920x1080
  5. Cambiar las propiedades en en monitor secundario (LVDS1)
    1. LVDS1 > Position > X: 1920 (Tomamos el valor X del punto 4.1)
  6. Cerra sesión y entrar nuevamente
Así tendrás tu monitor VGA como primario y el de tu Notebook como secundario. Esto también sirve para PC's con 2 monitores.

Acá un screenshot:




Tuesday, May 22, 2012

Get new and modified files from n days ago

This is another way to get new and modified files from your project.

If you use cvs system, you need to export files first

(using svn)*
svn export files/path export/path

* Maybe you need to use before
svn add * --force

Go to export/path and get the last new/modified files you want
cd export/path
find . -mtime -N -ctime -N | cpio -vdump ../final_files


N are the days that you want to get, so if you need to get files from 7 days ago then you write:
find . -mtime -7 -ctime -7 | cpio -vdump ../final_files

Monday, May 07, 2012

Codeigniter: multifile_array upload helper

If you need to use multiple files for upload in a form, but using array names (like files[]) with Codeigniter is not possible. Besides, $_FILES schema is not compatible with $this->upload->do_upload().
With this helper you can use this kind of names for make it possible. Only use array name for all files in a form and use the helper:

In view
<form...
<input type="file" name="f_file[]">
<input type="file" name="f_file[]">
...
</form>

In controller
$this->load->helper('upload');
multifile_array();
foreach ($_FILES as $file => $file_data)
  $this->upload->do_upload($file);

Download multifile_array upload helper

Wednesday, March 28, 2012

Codeigniter + HMVC + Namespaces (part 2)

Now I'm going to continue with CI+NAMESPACES+HMVC transformation. (See part 1)

Codeigniter + Namespaces + HMVC

Working with namespaces in hmvc is same that part 1. The diference is how we load a namespaced module:

namespace controllers\blog;

class user extends \MX_Controller {
  public function get_comments($id_user){
     /* Code */

     //[1]
     $module = $this->load->module('namespace\modulename/classname');
     //[2]
     $module->method($x, $y);

     // OR [3]
     \Modules::run('namespace\modulename/classname/method', $x, $y);
  }
}
$_ns = __NAMESPACE__;

How to load a module
  1. Load module into a variable. The format is namespace\[backslash]modulename/[slash]classname
  2. Call the module method
  3. Run module method statically
Download HMVC extension patch

Download HMVC module example

Tuesday, March 06, 2012

Codeigniter + HMVC + Namespaces (part 1)

Codeigniter 2.1.0 is PHP4 compatible, so it can't work with namespaces.

I work with HMVC extension, and I see that working with it and without namespaces it's a little clumsy (for example class collisions) so, I decided to modify Codeigniter and hmvc extension to work with namespaces for controllers.

Both solutions are independents. First I'm going to explain how to make it possible in Codeigniter only.

Codeigniter + Namespaces

namespace controllers\blog //[1]

class user extends \CI_Controller { //[3]
  public function get_comments($id_user){
     // Code  
  }
}
$_ns = __NAMESPACE__; //[2]

What is new here?
  1. You can see namespaces declaration, obviously [1]
  2. The new (required) variable. It is used by patch [2]
  3. As Codeigniter doesn't work with namespaces we must use a global namespace [3]
Anything else?

No! How easy!

Well, one more thing is required. Replace the system/core/Codeigniter.php file with the patch.
If you are afraid to do it, I have modified 2 lines only.