1.0.0 • Published 9 years ago

hr.model v1.0.0

Weekly downloads
16
License
-
Repository
github
Last release
9 years ago

hr.model Build Status

Data modelling utility

Installation

$ npm install hr.model

Documentation

Creation

Create a new model by extending the default Model:

var Model = require("hr.model");

var MyModel = Model.extend({

});

Create an instance with attributes using:

var post = new Post({}, {
    title: "My Post"
});

Attributes

Default values are defined in the defaults property of the model:

var Post = Model.extend({
    defaults: {
        title: ""
    }
});

Set an attribute using .set:

post.set("title", "My Awesome Posts");

Get an attribute using .get:

post.get("title");

Depp attribtes can be use:

post.set("count", {
    "likes": 0,
    "tweets": 0
})

post.set("count.likes", 500);

post.get("count")
// -> { "likes": 500, "tweets": 0 }

post.get("count.likes")
// -> 500

Events

// Triggered for every set of modifications
post.on("set", function() { ... });

// Triggered for every change on attributes
post.on("change:title", function() { ... });

Example with React

var React = require('react');
var Model = require("hr.model");

var User = Model.extend({
    defaults: {
        username: null,
        permissions: {
            read: true,
            write: false
        }
    }
});

var UserItem = React.createClass({
    componentWillMount : function() {
        this.props.user.on("change", this.forceUpdate.bind(this));
    },
    componentWillUnmount : function() {
        this.props.user.off("change");
    },
    render: function() {
        return (
            <div className="user">
            Username is {{this.props.user.get('username')}}
            </div>
        );
    }
});