Ten reasons for turning to Spine.js

2012/07/25 08:10
Reading number 392

In the past few years, I have gradually shifted my interest from desktop to web. I am fascinated by the way that apps can be accessed with a browser on any device. I have experience in HTML, CSS, Flash and PHP, so I am very familiar with this field -- but I have only done websites, not web apps. I began to have a deep understanding of Rails, and immediately fell in love with it. The quick response of Flash, which I am familiar with, was replaced by page loading. So I turned to Javascript.

Like the new kids in the school, I can't tell who is who about the framework. I searched and found Backbone.js It can be seen almost everywhere, so I assume it is a standard. A few months later, I found it was not for me-- Backbone.js  Lack of clear direction of use. Every tutorial I have read uses a different structure, which makes it too easy to ignore conventional and effective design patterns.

Start learning  Spine.js I spent one night reading through the user manual and testing its sample app, and everything I saw looked good. That night, I went to bed with a bright face, even though it was really hard for me to sleep, because I couldn't wait to use it. What makes me so excited? This is the following reason:

1. Clear structure

Spine.js Follow the MVC pattern. All applications I write follow the MVC architecture, so I soon know how to use Spine.js to organize my application structure. This sense of deja vu is great. This makes it clear which class is doing what and which class is activated.

2. Model is model

Backbone.js also has models, but it is clumsy, because some collections are essentially model arrays, which can query APIs and assign values to themselves with results. The Spine.js model is very similar to the Rails model. A model can be instantiated to render records, but it also has class level methods to get results from APIs. These methods return results instead of producing an array, so we don't need to consider where the class is active. Because collections are instances, many examples I have seen regard them as singletons. As a result, those who learn Backbone.js and follow the examples also learn to write unpredictable code.

3、Spine.app

When using Backbone.js, I found that every time I created a new class, I would copy/paste code. I began to miss the generator I was used to in Rails. With just one command, I can generate a new class with specs based on the template, which saves me several years of development life. "Developing Backbone. js generator" continues to stay on my todo list, but I never started here.

Spine. app generates a file. Just one line, I create a new class with a spec, just like Rails.

4. Dynamic record

This is just like the crazy black magic, but it solves a problem I encountered in Backbone.js. Suppose you get a record from a view you applied. Then you get and update the same record in different views. In Spine.js, both records will be updated at the same time. You don't have to worry about keeping them in sync. When I read this, I was moved by it.

5. Element Hash

When using Backbone.js, I always find that I manually assign variables to embed elements in the rendering method of each view, and repeat the same code for each element -- there are many template files. In Spine.js, it has element hash, key is selector, and value is variable name. Just like Backbone.js event hash, all your elements will be mapped, which is clear and concise.

6. Method update

When I used Flash, optimization was the key to survival. If I forget to remove an event listener, my app will overflow memory, just like an application that is less than maintained. Because of this, I have included a method in each class to unregister and remove all event listeners. Spine.js has this function built in.

7. Routes in the controller

There is no Router class in Spine.js. This function belongs to the controller class. In any controller, I can navigate to a new location and react to it. Other controllers can also respond to this new position. Now it is unnecessary to create a routing instance.

8. Model Adapter

By default, Spine.js stores the model in memory, but there are two adapters that can be applied to any model class -- Ajax and Local. Simply inherit these adapters, and your data can be stored in remote databases or use HTML5 local storage APIs. All of these functions require only one line of code.

9. Get model from HTML element

This is another problem I encountered in Backbone.js. I will instantiate a view and bind it to a model. When I need to reference data without accessing the view instance, I am not so lucky. Spine.js provides a jQuery plug-in to access the element model. Just call the data method on the element to get the corresponding model.

10. Log

 

Spine.js is born with a lightweight convenient logging module. You can call the log method in any controller. It will add a series of prefixes and output to the console.

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
two Collection
zero fabulous
 Back to top
Top