Spine.js vs Backbone.js

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 ) 立即下载

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. 


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

相关推荐

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的博客 259

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 1284

从原理到实践:MIPI D-PHY v1.2的HS-Deskew功能全解析(附时序图详解)

本文深入解析MIPI D-PHY v1.2的HS-Deskew功能,从技术背景到实现机制,详细介绍了其在2.5Gbps高速传输中的关键作用。通过时序图详解和实测数据,帮助工程师理解并应用这一技术,解决信号完整性问题,提升系统可靠性。

weixin_30300225的博客 320

STM32F1的USB接口-自定义HID

设置USB VID:1155 PID :22352。

chilian12321的博客 842

【Js源码】Spine.js

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

It_sharp的博客 969

基于NARX和NARX-LSTM的模型辨识与MPC控制系统调试与优化

本文针对两输入单输出非线性系统,提出基于NARX和NARX-LSTM的模型辨识与MPC控制优化方案。系统输入为固定量u1和可调量u2(8.5-140),输出目标为-0.58。现有模型存在辨识精度不足、历史输出依赖过重、预测偏差等问题。解决方案包括:数据差分处理改善非平稳性、调整延迟阶数优化模型结构、引入注意力机制替代手动加权、改进损失函数消除预测偏差。MPC控制器将结合优化模型,考虑控制变量步长约束(≥2),实现输出稳定控制。通过系统性的模型调试与控制算法优化,最终形成完整的非线性系统辨识与控制框架。

huanghm88的专栏 137

【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 1555

桌面运维工程师面试题汇总(高频完整版)

岗位,覆盖:电脑硬件、系统安装与故障、网络、办公软件、安全、AD域、打印机、日常排错、服务意识。

老韩的Linux云计算架构师进阶之路 981

医院批量排班实战指南(三)日期列表生成与时段批量操作

在上一篇《批量排班功能实现指南(二):日期与时段设置》中,我们完成了日期范围选择和重复规则配置的界面搭建,实现了从第一步到第二步的跳转功能。现在我们已经可以选择日期范围和重复规则,但还缺少最关键的功能——根据规则生成日期列表并进行时段配置。本篇将继续深入批量排班的核心功能——日期列表生成与时段批量操作。根据日期范围和重复规则智能生成日期列表为每个日期配置上午、下午、夜诊时段批量时段操作提升配置效率日期列表的展示与管理。

低代码布道师的博客 1355

学习使用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" /> &...

丷寒枫丷 850

ESP32设备驱动-MLX90393磁场传感器驱动

MLX90393 磁场传感器可以在运行时重新编程为不同的模式和不同的设置。 该传感器使用 Melexis 专有的 Triaxis® 技术提供与沿 XYZ 轴感应的磁通密度成比例的 16 位输出,还提供温度输出信号。 这些数字值可通过 I2C 和 SPI 获得,其中 MLX90393 是总线上的从设备。

视觉与物联智能 2371

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

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

phillip318的专栏 774

第29章:PCL PrincipalCurvatures 曲率估计

PrincipalCurvatures(主曲率估计)是一种用于计算点云表面曲率的算法。曲率描述了曲面局部的弯曲程度,是表面几何特征的重要指标。在点云处理中,估计表面的曲率对于形状分析、物体识别、表面重建等任务非常重要。主曲率是曲面上某一点的最大和最小曲率方向。通过计算点云的主曲率,我们可以获得点云中每个点的局部形状特征。通常情况下,主曲率包括两个主要参数:最大主曲率(最大曲率方向上的曲率)和最小主曲率(最小曲率方向上的曲率)。优点缺点提供了对点云表面局部形状的深入分析。

yumeiguo的博客 217

BLDC&PMSM电机控制器硬件设计工程(一)概述&需求&规划

这里是一个专注于电机控制器硬件工程的开放社区与知识仓库。我们的核心使命是分享、交流与共创,致力于解开无刷直流电机(BLDC)和永磁同步电机(PMSM)中、大功率控制器硬件设计,包括驱动IC选型,电流传感器选型,控制IC,母线电容计算,功率模块(IGBT、MOS管)选型以及大功率功率模块驱动电源设计。功率等级: 重点关注中等功率(如500W-10kW) 并向大功率(10kW以上) 延伸,探讨对应的功率器件(如MOSFET, IGBT, SiC)选型与驱动。

小幽余生不加糖 660

js框架

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

weixin_30835923的博客 172

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

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

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

最后一个BUG留给我 1712

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

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

frank1998819 316

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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

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

gitblog_01141的博客 890

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

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

yyf1990cs的博客 3843

动画开发之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的博客 161

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

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

cpongo5 348

导入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
博客等级 码龄20年 415粉丝 159原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址