Skip to content

Instantly share code, notes, and snippets.

@cmilfont
Created September 24, 2011 17:05
Show Gist options
  • Select an option

  • Save cmilfont/1239555 to your computer and use it in GitHub Desktop.

Select an option

Save cmilfont/1239555 to your computer and use it in GitHub Desktop.

Revisions

  1. cmilfont revised this gist Sep 24, 2011. 1 changed file with 74 additions and 0 deletions.
    74 changes: 74 additions & 0 deletions workflow.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    (function($){

    var controller;
    var templateConfiguration = {};

    function renderConfig(objeto, lista) {
    var json = {};
    json[objeto] = lista || [{}]
    return json
    }

    var callback = function(lista) {
    $("#top").html("").render(
    renderConfig(templateConfiguration.spriteCollection, lista)
    , templateConfiguration.sprite
    , templateConfiguration.url);

    $("a").click(function(){
    if($(this).text() === "Excluir") {
    controller.delete({
    id: $(this).attr("name")
    }, function(){
    controller.list({}, callback);
    }, errorHandler)
    }

    if($(this).text() === "Editar") {
    controller.show(
    {id: $(this).attr("name")},
    function(objeto) {
    $("form").populate(objeto);
    }
    )
    }
    return false;
    });

    };

    $.fn.extend({
    workflow: function(controllr, templateConfig) {
    controller = controllr;
    templateConfiguration = templateConfig;
    var $form = $(this);

    $.compileTemplates(templateConfiguration);
    controller.list({}, callback);
    $("input#[name='button']").click(function(){
    var json = $form.getJSON();
    if(json.id) {
    controller.update(json, function(){
    controller.list({}, callback)
    }, errorHandler)
    } else {
    controller.create(json, function(){
    controller.list({}, callback)
    }, errorHandler)
    }
    $("textarea,input:not([name='button'])").val("");
    });

    }
    });
    })(jQuery);

    var errorHandler = function() { console.log("Errors", arguments) }
    var cursosController = new CursosController;

    $(function(){
    $("form").workflow(
    cursosController,
    {url:"template.html", sprite:"curso", spriteCollection: "cursos"}
    );
    })
  2. cmilfont revised this gist Sep 24, 2011. 1 changed file with 20 additions and 6 deletions.
    26 changes: 20 additions & 6 deletions listar.js
    Original file line number Diff line number Diff line change
    @@ -14,12 +14,26 @@ CursosController.prototype.routes = {
    }
    }

    var errorHandler = function() { console.log("Errors", arguments) }
    var cursosController = new CursosController;

    var json = $("form").getJSON();
    var callback = function(cursos) {
    var callback = function(cursos) {

    $("#top").html("").render({
    cursos: cursos
    }, "curso", "template.html")
    cursos: cursos || [{}]
    }, "curso", "template.html");

    $("a").click(function(){
    if($(this).text() === "Excluir") {
    cursosController.delete({
    id: $(this).attr("name")
    }, function(){
    cursosController.list({}, callback);
    }, errorHandler)
    }
    return false;
    });

    };
    var errorHandler = function() { console.log("Errors", arguments) }
    var cursos = new CursosController;
    cursos.list({}, callback)
    cursosController.list({}, callback)
  3. cmilfont revised this gist Sep 24, 2011. 1 changed file with 25 additions and 0 deletions.
    25 changes: 25 additions & 0 deletions listar.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    function CursosController() { DJR.call(this); }
    CursosController.prototype.routes = {
    "list": {
    url: "/cursos/", method: "GET"
    },
    "create": {
    url: "/cursos/", method: "POST"
    },
    "edit" : {
    url: "/cursos/:id/", method: "PUT"
    },
    "delete": {
    url: "/cursos/:id/", method: "DELETE"
    }
    }

    var json = $("form").getJSON();
    var callback = function(cursos) {
    $("#top").html("").render({
    cursos: cursos
    }, "curso", "template.html")
    };
    var errorHandler = function() { console.log("Errors", arguments) }
    var cursos = new CursosController;
    cursos.list({}, callback)
  4. cmilfont revised this gist Sep 24, 2011. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions djr.js
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,16 @@ Array.prototype.add = function(el) {
    this[this.length] = el;
    return this;
    };

    if(typeof Array.prototype.each == "undefined") {
    Array.prototype.each = function(fn) {
    for(var index = 0; index < this.length; index++) {
    fn(this[index], index);
    }
    return this;
    }
    }

    function toMap(object) {
    var arr = [];
    for(var item in object)
  5. cmilfont created this gist Sep 24, 2011.
    79 changes: 79 additions & 0 deletions djr.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    Array.prototype.add = function(el) {
    this[this.length] = el;
    return this;
    };
    function toMap(object) {
    var arr = [];
    for(var item in object)
    arr[arr.length] = {key: item, value: object[item]};
    return arr;
    };

    var DJR = function() {
    this.data = "";
    this.error = {};
    this.format = ".json";

    this.formatURL = function(url, params) {
    toMap(params).each( function(param) {
    url = url.replace((new RegExp(param.key)).source, param.value);
    });
    return url;
    };
    this.callbackDefault = function(data) {
    this.data = data;
    };
    this.errorDefault = function(error) {
    this.error = error;
    };
    this.ajax = function(object, callback, error, method, url, params) {
    var self = this;
    var params = params || [];
    params["\(.:format\)"] = this.format;

    if(method === "")
    method = "GET";

    toMap(object).each( function(param) {
    params[":" + param.key] = param.value;
    });
    if(method !== "GET") {
    object = JSON.stringify(object);
    if (object && object.length === 2)
    object = null;
    }

    jQuery.ajax({
    context : self,
    data : object,
    cache : false,
    dataType : 'json',
    error : error,
    contentType : "application/json",
    headers : {"Content-Type":"application/json", "Accept":"application/json"},
    success : callback,
    type : method,
    url : this.formatURL(url, params)
    });
    };
    for (var action in this.routes) {
    this[action] = function(act) {
    return function(object, callback, error) {
    if(typeof object === "function") {
    callback = object;
    error = callback;
    object = {};
    }
    var localCallback = callback || this.callbackDefault;
    var localErrorHandler = error || this.errorDefault;
    this.ajax(object,
    localCallback,
    localErrorHandler,
    this.routes[act].method,
    this.routes[act].url);
    return this;
    }
    }(action);
    }

    };