0.3.40 • Published 11 years ago

linen v0.3.40

Weekly downloads
14
License
BSD
Repository
github
Last release
11 years ago

Linen (line-in) maps API's to bindable objects, and collections. At classdojo, we use linen to abstract our API from front-end, so we don't necessarily depend on any sort of API while developing new components. This allows us to rapidly build prototypes which can be wired up later.

Here's an example person schema:

var linen = require("linen")();

linen.addSchema({

  // name of the schema - gets referenced by 
  // linen.model("person")
  name: "person",

  //fields for the person. Keep in mind that model properties
  //get *validated* against their coresponding field. 
  fields: {
    firstName: "string",
    lastName: "string",

    // virtual value - doesn't actually exist in the API
    fullName: {
      $get: function(model) {
        return model.get("first_name") + " " + model.get("last_name");
      },
      $set: function(model, value) {
        var nameParts = String(value).split(" ")
        model.set("first_name", nameParts.shift());
        model.set("last_name", nameParts.join(" "));
      },
      $bind: ["first_name", "last_name"]
    },

    // fetches GET /people/:personId/friends when
    // person.bind("friends").to(fn) is called
    friends: [{
      $ref: "person",
      $fetch: function(payload, next) {
        transport.fetch(payload.method, "/people/" + payload.model.get("_id") + "/friends", next);
      }
    }]
  },

  //fetches GET /people/:personId when
  //person.bind(property).to(fn) is valled
  fetch: function(payload, next) {
    transport.fetch(payload.method, "/people/" + (payload.model.get("_id") || ""), next);
  }
});

To use the schema, simply call the following:

var person  = linen.model("person"),  //creates a new person
existingPerson = linen.model("person", "someId"); //some ID

Linen works by overriding the bind method to fetch from any API you have setup, so when you call:

//calls GET /people/someId
existingPerson.bind("firstName").to(function(name) {
  console.log(name); 
}).now();

The existingPerson will asynchronously call .to(fn) when it's been loaded from the server. This is useful when data-binding to any sort of UI component, such as rivets.js, or paperclip.js.

Here's another example of data-binding to linen models:

// GET /people/someId/friends
existingPerson.bind("friends").to(function(friends) {

  // GET /people/friendId
  friends.at(0).bind("firstName").once().to(function(name) {
  }).now();
}).now();

The above examples make it easy to abstract models from any API. To demonstrate this, here's an example of using dummy data:

var existingPerson = new bindable.Object({
  firstName: "Ron",
  lastName: "Burgundy",
  friends: [
    new bindable.Object({
      firstName: "Brian",
      lastName: "Fontana"
    }),
    new bindable.Object({
      firstName: "Brick",
      lastName: "Tamland"
    }),
    new bindable.Object({
      firstName: "Champ",
      lastName: "Kind"
    })
  ]
});


existingPerson.bind("firstName").to(function(name) {
  console.log(name); //Ron 
}).now();

existingPerson.bind("friends").to(function(friends) {
  friends.at(0).bind("firstName").once().to(function(name) {
    console.log(name); //Brian
  }).now();
}).now();

API

linen()

Returns a new linen instance

linen.addSchema(definition)

adds a new schema

definition

Inspired by mongoose:

linen.addSchema({
  firstName: {

  }
});

linen.model(schemaName, modelId )

returns a new, or existing model

0.3.40

11 years ago

0.3.39

11 years ago

0.3.38

11 years ago

0.3.37

11 years ago

0.3.36

11 years ago

0.3.35

11 years ago

0.3.34

11 years ago

0.3.33

11 years ago

0.3.32

11 years ago

0.3.31

11 years ago

0.3.30

11 years ago

0.3.29

11 years ago

0.3.28

11 years ago

0.3.27

11 years ago

0.3.25

11 years ago

0.3.24

11 years ago

0.3.23

11 years ago

0.3.21

11 years ago

0.1.43

11 years ago

0.3.19

11 years ago

0.3.18

11 years ago

0.3.17

11 years ago

0.3.16

11 years ago

0.3.15

11 years ago

0.3.13

11 years ago

0.3.10

11 years ago

0.3.8

11 years ago

0.3.7

11 years ago

0.3.6

11 years ago

0.3.5

11 years ago

0.3.4

11 years ago

0.3.2

11 years ago

0.1.42

11 years ago

0.1.40

11 years ago

0.1.39

11 years ago

0.1.38

11 years ago

0.1.36

11 years ago

0.1.35

11 years ago

0.1.34

11 years ago

0.1.33

11 years ago

0.1.29

11 years ago

0.1.28

11 years ago

0.1.27

11 years ago

0.1.25

11 years ago

0.1.23

11 years ago

0.1.21

11 years ago

0.1.20

11 years ago

0.1.19

11 years ago

0.1.18

11 years ago

0.1.17

11 years ago

0.1.16

11 years ago

0.1.15

11 years ago

0.1.14

11 years ago

0.1.11

11 years ago

0.1.10

11 years ago

0.1.8

11 years ago

0.1.7

11 years ago

0.1.6

11 years ago

0.1.5

11 years ago

0.1.1

11 years ago

0.0.37

11 years ago

0.0.35

11 years ago

0.0.34

11 years ago

0.0.33

11 years ago

0.0.32

11 years ago

0.0.30

11 years ago

0.0.28

11 years ago

0.0.27

11 years ago

0.0.26

11 years ago

0.0.25

11 years ago

0.0.24

11 years ago

0.0.23

11 years ago

0.0.22

11 years ago

0.0.21

11 years ago

0.0.20

11 years ago

0.0.19

11 years ago

0.0.18

11 years ago

0.0.17

11 years ago

0.0.16

11 years ago

0.0.14

11 years ago

0.0.13

11 years ago

0.0.12

11 years ago

0.0.11

11 years ago

0.0.10

11 years ago

0.0.9

11 years ago

0.0.8

11 years ago

0.0.7

11 years ago

0.0.6

11 years ago

0.0.2

11 years ago