Giter Site home page Giter Site logo

backbone.meninges's Introduction

Meninges models let you load deep domain models into backbone models by declaring associations (rather than coding the relationships manually). Meninges also provides a FormView that binds blur events on html form inputs to a function refreshing the backing MeningesModel from the form values.

To use it

  • Include meninges.js in your page:
    <script type="text/javascript" src="js/meninges-model.js"></script>
    <script type="text/javascript" src="js/meninges-view.js"></script>
  • Define your Models
    MyApp.Country = Backbone.Model.extend();
    MyApp.Author = Backbone.MeningesModel.extend({
      associations: {
        "country" : {model: "MyApp.Country"}
      }
    });
    MyApp.Links = Backbone.Collection.extend({
      model: MyApp.Link
    });
    MyApp.Link = Backbone.Model.extend();
    MyApp.Book = Backbone.MeningesModel.extend({
      associations: {
        "author": {model: "MyApp.Author"},
        "links": {model: "MyApp.Links"}
      }
    });
  • Load your json into your models
    data = {
      id: 1,
      title: "Le Menon",
      author: {
        is_dead: true,
        name: "Platon",
        gender: "male",
        country: {
          name: "greece",
          continent: "europe"
        }
      },
      links: [
        {type: "buy", uri: "http://amazon.fr/123"},
        {type: "read", uri: "http://livresenligne.fr/lemenon"}
      ]
    };

    var book = new MyApp.Book(data);
  • Nested objects are automatically loaded into MeningesModel (as long as they're defined)
    book.get("author").get("country").get("name") //greece
  • toJSON will include the json of the nested meninges models

Put a form in front of your model

  • Extend Backbone.MeningesView
    MyApp.BookView = Backbone.MeningesView.extend({
      render: function () {
        var html = '<select name="author.country.continent" class="meninges">';
        $(this.el).html(html);
        return this;
      }
    });
  • Instantiate it
    bookView = new MyApp.BookView({model: this.book});
    // ... render the view and append to a html node.
  • From then on, user input should be synchronised to MeningesModel as the user leaves the input fields (blur).

  • Follow this syntax to bind input fields to model attributes

    <input name="author.country.name" class="meninges" type="text" />
    // binds the input to this.model.get("author").get("country").get("name")

    <input name="links:0.url" class="meninges" type="text" />
    // binds the input to this.model.get("links").at(0).get("url")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.