JavaScript MVC 微型架构:Gillie
jopen
11年前
Gillie是一个轻量级的JavaScript MVC框架4k,启发自Backbone。它提供了一些有用的方法来执行RESTful HTTP请求,可以让您轻松地使用模型,视图和处理器来构建您的应用程序。在另一方面提供了一个事件的API,使用它可以使你的视图监听模型事件,然后触发相应的动作,遵循观察者设计模式。
Model
var TodoModel = Gillie.Model.extend({ url: 'http://localhost/api/' create: function( event ) { this.Post( 'todo/', event ); } }); // Crete model instance var todoModel = new TodoModel();View
var TodoView = Gillie.View.extend({ initialize: function() { // Bind to 'todo.create' event todoModel.on( 'todo.create', this.onCreate ); } , onCreate: function( instance, response ) { alert( instance.get( 'title' ) + '. Has been saved' ); } }); // Create view instance var todoView = new TodoView();Handler
var TodoHandler = Gillie.Handler.extend({ events: { 'keypress #new-todo': 'createOnEnter' } , createOnEnter: function( e ) { var enterKey = 13 , currentTarget = e.currentTarget; if ( e.which != enterKey ) return; var title = $( target ).val(); todoModel.set( 'title', title ) .create( 'todo.create' ); } }); // Create handler instance var todoHandler = new TodoHandler();