Home

Awesome

Knockout.model plugin

Copyright 2011, Alisson Cavalcante Agiani

Licensed under the MIT license.

http://github.com/thelinuxlich/knockout.model/MIT-LICENSE.txt

Date: Fri Mar 04 14:00:29 2011 -0300

Files:

Dependencies:

Howto

Model Methods

Example(see docs for more details):

var Employee = Ctor(KoModel, function(super) { // inheriting KoModel to boost your own models!
    this.__urls = {
        "index": "http://#{window.location.host}/employees",
        "show": "http://#{window.location.host}/employees/:id",
        "create": "http://#{window.location.host}/employees/post",
        "update": "http://#{window.location.host}/employees/put",
        "destroy": "http://#{window.location.host}/employees/delete"
    }

    // We won't send status_text attribute to the server
    this.__transientParameters = ["status_text"];

    // An example of callback for automatically setting the model id after create
    this.__afterHooks: {
        "create": function(response) {	this.id(response.id) }
    }

    this.id = ko.observable("");
    this.name = ko.observable("John Doe");
    this.surname = ko.observable("");
    this.fullname = ko.computed(function() {
        return this.name() + " " + this.surname();
    }, this);
    this.birth_date = ko.observable("");
    this.address = ko.observable("");
    this.phone = ko.observable("");
    this.status = ko.observable("E");
    this.status_text = ko.computed(function() {
         if(this.status() === "E") {
            return "enabled"; 
         } else {   
            return "disabled";
         }
    }, this);
    super.init.call(this); // if you want the this.__defaults applied to your model instance, call super.init.call(this)

    this.validate = function() {
        return(this.name() !== "" && this.surname() !== "" && this.birth_date() !== "");
    }
});