Coder Social home page Coder Social logo

magicbill / backbone-view-model Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tommyh/backbone-view-model

0.0 2.0 0.0 133 KB

Backbone.ViewModel - Keep view's business logic in a ViewModel and your persistance logic in your Model

License: MIT License

backbone-view-model's Introduction

Why do I need a Backbone.ViewModel?

Backbone.Model's are great at storing the state of your objects and persisting them back to your server. But as your Backbone.View's become more complex, it's useful to have a model to store all view related attributes. These view attributes should be stored on a separate model than persistence attributes for 2 reasons:

  • Separation of concerns
  • If you store view attributes on the same model as your persistence model, when you call .save() on that model, the view attributes will be sent to the server too, eek!

Usage

Let's say you have a fairly standard Model and View:

// defining your classes
var Tweet = Backbone.Model.extend({});
var TweetView = Backbone.View.extend({});

// creating an instance of your model
var myTweet = new Tweet({text: "I love backbone!"});

You define a Backbone.ViewModel class with a computed_attributes object:

var TweetViewModel = Backbone.ViewModel.extend({
	computed_attributes: {
		"truncated_text" : function(){
			return this.get("source_model").get("text").substring(0,10) + "…";
		},
		"escaped_text" : function(){
			return encodeURIComponent(this.get("source_model").get("text"));
		}
	}
});

Initialize your ViewModel and pass your persistence model as source_model:

var myTweetViewModel = new TweetViewModel({
	source_model: myTweet
});

Now, pass your ViewModel to your View:

var myTweetView = new TweetView({
	model: myTweetViewModel
});

When your ViewModel is initialized or whenever any attribute on your source_model changes, all of the computed_attributes will be processed and set on your ViewModel:

// The view attributes are set on the ViewModel
myTweetViewModel.get("truncated_text") // => "I love bac…"

// They can be used easily in your View Template
{{ truncated_text }} <a href="#">View more</a>

Installation

To install, include the src/backbone-view-model.js file in your HTML page, after Backbone and it's dependencies.

Testing

This project uses QUnit for it's automated tests. If you'd like to contribute, please:

  • make sure the tests are green: backbone-view-model/test/index.html
  • add tests for your new feature/fixes

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.