Spine.js vs Backbone.js

JavaScript的MVC框架Spine.js.zip Spine.js 是一个用于构建 JavaScript Web 应用的轻量级框架。Spine 可让你使用 MVC 的框架思路来开发Web应用。 标签:Spine 立即下载

Spine and Backbone are two Javascript MVC frameworks that look very similar on the surface but under the hood there are key differences. The purpose of this article is to highlight these differences and hopefully make it easier to choose which framework fits your needs.

The basics

Backbone is written in Javascript and was created by Jeremy Ashkenas in the year 2010. Spine came later and was highly inspired by Backbone, it is created by Alex MacCaw and is written in CoffeeScript.

Backbone was very successful right from the start and has been used by many established companies such as 37signals, Fog Creek Software, LinkedIn and many others. Spine has gained some attraction, specially by CoffeeScript developers, but not nearly as much as Backbone.

Spine size is 7k, it aims to simplify MVC and integrates well with CoffeeScript. Backbone size is 5.6k and it depends on the utility library Underscore.js ( 4k ) that makes up for some of Javascript missing features.

Positive by default

In the beginning Spine implemented a concept called asynchronous user interfaces. The way it works is that the user interface is updated when models are saved without waiting for the server response, expecting the result to be positive. This made Spine apps snappier and is explained well by the creator of Spine in his article about async uis.

Backbone introduced the same concept in version 0.9 and the models are now also positive by default. It’s still possible to turn this behavior off in Backbone by setting the wait option to true in the methods save, create and destroy.

Example:


contact.save({ wait: true });

Models vs. Collections

Backbone has Models and Collections but in Spine there are only Models.

Spine is similar to Active Record in Rails where static methods operate on the list of records while instance methods work on single record instances. This makes Spine simple for doing basic things such as adding a contact to a contact list.

Example:


var Contact = Spine.Model.sub();
Contact.configure("Contact", "name", "email");
Contact.extend( Spine.Model.Ajax );


// Usage

var contact = new Contact({  
	name: "Sven Svensson", 
	email: "sven@svensson.se" 
});

contact.save();

The save operation in this example syncs the model to the server and adds the record to the list of contacts. Let’s next accomplish the same thing using Backbone.

Example:


var Contact = Backbone.Model.extend({});

var Contacts = Backbone.Collection.extend({
	model: Contact,
	url: "contacts"
});


// Usage

var contacts = new Contacts();
var contact = new Contact({  
	name: "Sven Svensson", 
	email: "sven@svensson.se" 
});

contacts.create( contact );

As you can see there are differences between the two so let’s dive a bit deeper.

Attributes

Notice this line in the Spine model example.


Contact.configure("Contact", "name", "email");

Spine attributes that are synced to the server need to be configured so they are not mixed with other properties on the model that are only stored in memory.

Example:


Contact.configure("Contact", "name", "email");
contact.name = “John Johnson”;
contact.email = “john@johnson.com”;
contact.onlyInMemory = true;

In Backbone attributes are not stored directly on the model and are accessible through get and set methods.

Example:


contact.set({
	name: “John Johnson”,
	email: “john@johnson.com”
});

contact.onlyInMemory = true;

This means that Spine models are more static because they need to be defined beforehand but Backbone models are dynamic and can be changed on the fly. If you need models that have dynamic attributes Backbone is a better friend.

Another advantage of the setter method in Backbone is that it fires both change and change:attribute events when setting attributes. This is helpful in use cases where the user interface should be updated at the same time as attributes are changed.

Unique identifiers

Spine makes it impossible to change the name of the unique identifier to something else than id.

This is a problem for example with databases such as CouchDB and MongoDB where the name is usually _id but not id. To make this work with Spine there needs to some sort of conversion between the two names which is dirty.

Backbone has a property called idAttribute that makes it easy to switch the unique identifier name to something else than id.

Example:


var Contact = Backbone.Model.extend({
  idAttribute: "_id"
});

The models of Backbone also include an identifier called cid that is generated on the client before models are saved to the server and are specially useful for asynchronous user interfaces.

Strangely enough Spine models generate a client identifier that is set on the id before saving to the server and is overwritten as the model is synced for the first time. This is in my opinion very confusing and only increases the likelihood of introducing bugs.

Storing records

Spine stores records in a hash internally but Backbone uses both an array and a hash.

The Spine hash uses id as the hash key and are therefore super fast for retrieving records by id. The problem with this approach is that for most other cases the hash is slower, for example the methods count, toJSON, first, last need to convert the hash to an array before returning the result, this is slower than just using an array.

Backbone collections keeps the records array always sorted by default but that behavior is hard to reproduce using only a hash. To sort records in Spine the hash has to be converted to an array and sorted using the sort method of javascript arrays.

That brings us to the next part about helper methods.

Helpful Helpers

Backbone depends on the Underscore library as stated above. Underscore is mostly used by Backbone collections to provide many useful methods such as map, filter, pluck and many more can be quite useful.

Spine has few helper methods like select and findByAttribute but nothing as extensive as in Underscore . This is less of a problem in Spine for CoffeeScript developers as Coffee makes it easier to implement these kinds of operations.

Views vs. Controllers

Spine controllers and Backbone views serve the same purpose and bridge the gap between the HTML markup and the model layer. In a conventional server-side MVC framework this layer is most often referred to as controllers and in my mind that name makes more sense than calling it views because the markup represents the view.

Lets not get carried away and rather take a look at a simple Backbone view example.

Example:


var ContactView = Backbone.View.extend({

	events: { 
		"click li": "openContact" 
	},

	initialize: function() {
		contacts.bind( "reset", reset, this );
		contacts.fetch();
	},
	
	reset: function() {  
 	    this.contactList = this.$("#contact-list");

 	    // Do stuff
    }

});

// Usage
new ContactView({ el: $("#view").get(0) });

Spine controllers are almost identical.


var ContactView = Spine.Controller.create({

	events: { 
		"click li": "openContact" 
	},

	elements: { 
		"#contact-list": "contactList" 
	},

	init: function() {
		ContactModel.bind( "refresh", refresh );
		ContactModel.fetch();
	},

	refresh: function() {
		// Do stuff
	},
	
});


// Usage
new ContactView({ el: $("#view") });

In both implementation we have list of events, a constructor and methods. Spine additionally provides a way to define the elements used by the controller with the elements list, its a very much used shorthand and a nice-to-have.

Architecture

Another good thing about the Spine is that the library is broken into separate files. Spine core, ajax, local storage and routes are separate files so it’s possible to pick which one to include in our apps and avoid loading unnecessary code.

I think this is something that could be done with Backbone as its mainly one file that could be broken up to make things lean and cleaner.

Conclusion

Spine and Backbone are frameworks that provide structure to our javascript code and features that help us to focus on writing better web applications. I have used both these frameworks and been happy with the results in both cases.

As a conclusion I recommend when the key thing is simplicity, file size or CoffeeScript integration choose Spine. In most other cases I recommend using Backbone with Underscore as the tool for the job. 


Spine-EaselJS:使用 EaselJS 渲染 Spine 动画 Spine-EaselJS 使用 EaselJS 渲染 Spine 动画 Spine-EaselJS 脊柱动画示例 var stage = new createjs . Stage ( 'myCanvas' ) ; var dragon = new createjs . Spine ( "res/sceletons/dragon/dragon.atlas" , "res/sceletons/dragon/dragon.json" ) ; dragon . setTransform ( 300 , 220 , 0.5 , 0.5 ) ; dragon . onComplete = function ( ) { console . log ( dragon ) ; dragon . state . setAnimationByName ( 0 , 'flying' , true ) 立即下载

相关推荐

pixi-spine, 支持脊椎支持的Pixi.js 插件.zip

pixi-spine, 支持脊椎支持的Pixi.js 插件 pixi脊椎 pixi v3和 pixi v4的脊椎实现。用法预生成的文件如果只包含生成的文件,pixi脊椎将自身添加到pixi命名空间中:new PIXI.spine.Spine();基本示例var app

JavaScript 客户端 MVC 框架调查

简介   15 年前,许多人都使用 Perl 和 ColdFusion 之类的工具构建网站。我们经常编写可以在页面顶部查询数据库的脚本,对数据应用必要的转换,以及在同一个脚本底部显示数据。这类架构适合于向网站添加简单的 “Contact us” 表单。然而,随着应用程序变得更加复杂,这种方法无法进行相应的扩展来处理更大的复杂问题。大部分 Web 应用程序现在已经对模型-视图...

weixin_34306446的博客 257

backboneJS&nodeJS

backboneJS

【前端/Spine.js】不使用服务器形式来传递json和altas数据

说在前面 测试浏览器:chrome 81.0.4 其他工具:visual studio code 吐槽:用spine.js很不爽,因为必须得搭个支持xhr的服务器,就很烦;然后小改了下,这样测试起来就简单点 代码 主要是AssetManager这个类,给他添加个loadTextData的方法,参数依然仿照其他的函数AssetManager.prototype.loadTextData = function (path, data) { var _this = this; path = th

o0o 1279

【CubeMX】STM32G474 HRTIM 互补PWM与死区时间配置实战

本文详细介绍了如何使用STM32CubeMX配置STM32G474的高分辨率定时器HRTIM生成互补PWM波形并精确设置死区时间。通过实战步骤讲解时钟配置、死区计算与高级功能,帮助工程师在电机驱动和电源应用中实现精准控制,避免上下桥臂直通风险。

grpc6streamer的博客 724

Android WLAN STA/AP 并发

Android 9 引入了可让设备同时在 STA 和 AP 模式下运行的功能。对于支持双频并发 (DBS) 的设备,此功能让一些新功能得以实现,例如在用户想要启用热点 (softAP) 时不会中断 STA WLAN。

华南烹鱼宴 1247

【Js源码】Spine.js

前言 js的这些个MVC框架喜欢的不得了,可是看一遍又忘记了,无法领悟其要领,遂摘抄下来,希望能够加深理解。 摘抄 (function(){ var $,Controller,Events,Log,Model,Module,Spine,guid,isArray,MakeArray,moduleKeywords; //数组分割 var __Slice = Arr...

It_sharp的博客 969

若依框架整合Flowable:从零构建企业级流程中心

本文详细介绍了如何将若依框架与Flowable工作流引擎整合,构建企业级流程中心。通过环境配置、依赖添加、数据库设置等核心步骤,实现流程定义管理、任务处理服务等关键功能,并结合实际案例展示请假审批流程的实现与动态表单集成。文章还提供了性能优化和生产建议,帮助开发者高效构建稳定可靠的工作流系统。

weixin_28365523的博客 228

【AR.js】在AR.js中使用spine动画

说在前面 测试浏览器:Microsoft Edge(PC版本 97.0.1072.55) github库:AR.js AR系列文章:这里 go版本:go version go1.17.3 windows/amd64 three.js版本:r79(版本比较老) 其他:AR.js支持three.js。 github地址:这里 效果图 代码 <!DOCTYPE html> <meta name="viewport" content="width=device-width, user-s

o0o 1543

C# CAD2016 多边形顶点按方向重新排序

这样,通过以上步骤,我们找到了一个多边形的一个候选起始顶点(可能位于西北方向),并且已经获取了该多边形的边界框信息,这些信息对于后续判断顶点排列顺序及处理其他相关问题具有重要意义。完整地处理八个方向(东北、东南、西南、西北、北、南、东、西),我们可以创建一个方法来获取多边形相对于给定点的最近顶点及其对应的方向。对象(或类似的多边形表示方式),其中包含了多边形的顶点。//附送AI 图片无版权 随意用 龙年大吉大利。

转眼之间 一路有你 1355

Xilinx Vitis 2020.1头文件路径配置避坑指南:解决#include找不到文件的常见问题

本文针对Xilinx Vitis 2020.1开发环境中常见的#include头文件找不到问题,提供了系统的避坑指南。文章深入解析了Vitis的头文件搜索机制,对比了通过工程属性GUI、修改编译命令及使用环境变量三种配置方法的优劣与隐藏陷阱,并给出了高级场景的排查流程与工程结构最佳实践,帮助开发者高效解决路径配置难题。

weixin_28831399的博客 569

学习使用spin.js 完成 loading加载动画

Spin.js 的在线设计、演示及下载地址为:http://fgnass.github.io/spin.js/ 添加引用: <!-- bootstrap-4.1.3 --> <link rel="stylesheet" href="/Content/new_wechat/dist/bootstrap-4.1.3/css/bootstrap.min.css" /> &...

丷寒枫丷 847

泛微ecology9二次开发-附件上传接口实战:从表单创建到数据库存储解析

本文详细解析了泛微ecology9二次开发中的附件上传接口实战,从测试环境搭建、数据库表结构解析到接口代码实现,提供了全面的开发指南。重点介绍了文件上传的核心逻辑、常见问题排查及高级应用场景,帮助开发者高效实现附件上传功能并优化系统性能。

weixin_30836759的博客 477

【知识库】八款你不得不知的开源前端JS框架

angular.js Angular.JS是一个开源的JavaScript框架,最适于开发客户端的单页面应用。它实现了前端MVC架构,专注于扩展HTML功能,提供动态数据绑定(Data Binding),且能与其它框架(如jQuery)合作融洽。(项目详情) 项目主页: http://angularjs.org/代码托管地址: https://github.

phillip318的专栏 772

华为OD机试 - 文件存储系统的排序 - 深度优先搜索dfs(Java 双机位C卷 200分)

华为OD机试 双机位C卷题库疯狂收录中,刷题。

学Java,找哪吒 719

PCL 读取和保存点云

PCL (Point Cloud Library) 是一个用于处理 3D 点云数据的开源库。在处理点云数据时,常见的任务包括点云的读写和可视化。PCL 提供了强大的 IO 模块来读取和写入各种格式的点云文件,并且提供了可视化模块来对点云进行渲染和显示。点云的读写与显示是很多 3D 数据处理任务的基础步骤,通过这些步骤,可以对点云进行预处理、分析和操作。

纵马踏花向自由 1737

js框架

本文推荐了八款比较热门和经典的Javascript 开源框架,有兴趣的朋友可以看看~ Angular.js Angular.JS是一个开源的JavaScript框架,最适于开发客户端的单页面应用。它实现了前端MVC架构,专注于扩展HTML功能,提供动态数据绑定(Data Binding),且能与其它框架(如jQuery)合作融洽。(项目详情) ...

weixin_30835923的博客 172

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

八款你不得不知的开源前端JS框架

本文推荐了八款比较热门和经典的Javascript 开源框架,并给出了相关学习资料,有兴趣的朋友可以看看~angular.jsAngular.JS是一个开源的JavaScript框架,最适于开发客户端的单页面应用。它实现了前端MVC架构,专注于扩展HTML功能,提供动态数据绑定(Data Binding),且能与其它框架(如jQuery)合作融洽。(项目详情)项目主页: http://angula

最后一个BUG留给我 1712

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

八款你不得不知的开源前端JS框架(转)

本文推荐了八款比较热门和经典的Javascript 开源框架,并给出了相关学习资料,有兴趣的朋友可以看看~ angular.js Angular.JS是一个开源的JavaScript框架,最适于开发客户端的单页面应用。它实现了前端MVC架构,专注于扩展HTML功能,提供动态数据绑定(Data Binding),且能与其它框架(如jQuery)合作融洽。(项目详情)   项目主页...

frank1998819 315

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Spine.js入门指南:轻量级MVC库快速上手教程

Spine.js是一个轻量级MVC库,专为构建JavaScript应用程序设计。作为GitHub加速计划中的重要项目,Spine以简洁架构和高效开发流程著称,帮助开发者快速构建出色的Web应用。 ## 🚀 为什么选择Spine.js? ### ✨ 轻量级核心优势 Spine的核心库仅包含不到700行CoffeeScript代码,却提供完整的MVC架构支持。这种极致精简的设计带来三大优势: -

gitblog_01141的博客 886

createjs支持spine骨骼的简单封装(更新,加入换装)

spine的官网上有一个链接,给了用easel写的支持spine 的js文件 这里把他的例子的代码整理了一下,封装成一个简单的类来用。 使用方法就像这样: 1、创建人物 if (!role1) { role1 = new EaselSpine('./assets/skeleton/spineboy/spineboy.atlas', './assets/

yyf1990cs的博客 3840

动画开发之PIXI开发

简单的移动小游戏只要引入pixi.min.js就可以, 如果要用spine动画(龙骨也支持导出spine格式的)就要引入pixi-spine.js 如果还有声音的支持引入pixi-sound.js 学习网址: - 官网http://www.pixijs.com - APIhttp://pixijs.download/release/docs/index.html - 入门资料https:...

weixin_56071908的博客 702

为何转向Spine.js的十个理由

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34252090的博客 160

Spine.JS+Rails重客户端Web应用技术选型思路:『风车』架构设计

上周末,应邀在 Hacker News 上海聚会和 Ruby 上海活动上做了『风车』架构介绍的分享,在此感谢各位组织者和活动场地提供方。\风车这个项目开始于 2011 年 11 月份,之前叫做 Pragmatic.ly。从第一天开始我们基本上就定了大致的框架结构,在今天回头看,基本上整个架构都没有什么变化,可以算是个很成熟和很适合时代的方案,☺。\最近一两年,作为技术人员,我们都能很明显的感觉到前...

cpongo5 344

导入spine使用

运行环境 win7X64 ,unity5.3.4 1、加入需要的spine脚本及dll 2、导入spine动画,,里面有贴图,json文件,atlas文件(此文件必须加.txt后缀,否则unity认不到) 3、由于有了第一步,里面脚步对编辑器扩展,所以我们右键create多了一些选项,在2的目录下create -》spine Atlas.然后重命名。 4、create-》material

joson的博客 1万+
上一篇: Android将允许纯C/C++开发应用
下一篇: 链表的三种构造方法
xiaoding133
博客等级 码龄19年 415粉丝 159原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值