TodoMVC中的Backbone+MarionetteJS+RequireJS例子源碼分析之三 Views

來源:互聯網
上載者:User

標籤:

這個版本的TodoMVC中的視圖組織劃分比較細,更加易於理解,這也得益於Marionette為我們帶來了豐富的視圖選擇,原生的backbone只有views,而Marionette則有itemview, collectionview, compositeview 和layoutview.

js/templates.js

/*global define */define(function (require) {//這裡用了簡寫,因為require.js是CommonJS 模組,相當於 define(["require"],function(require){    ‘use strict‘;    return {        todoItemView: require(‘tpl!templates/todoItemView.tmpl‘),//tpl是requireJS的text.js外掛程式的擴充,!符號把templates/todoItemView.tmpl傳給tpl處理返回string內容,詳細可以查看js/lib/tpl.js定義        todosCompositeView: require(‘tpl!templates/todoListCompositeView.tmpl‘),//同上        footer: require(‘tpl!templates/footer.tmpl‘),//同上        header: require(‘tpl!templates/header.tmpl‘)//同上    };});

js/templates/head.tmpl

<h1>todos</h1><input id="new-todo" placeholder="What needs to be done?" autofocus>

js/views/head.js  這個view是用來實現 輸入建立新的todo的view視圖

/*global define */define([    ‘marionette‘,    ‘templates‘], function (Marionette, templates) {    ‘use strict‘;    return Marionette.ItemView.extend({        template: templates.header,//參考templates.js裡面的模板的定義        ui: {            input: ‘#new-todo‘//在Merionette中ItemView可以用ui來組織dom元素方便使用,返回的是jQuery對象        },        events: {            ‘keypress #new-todo‘: ‘onInputKeypress‘//原生的backbone的view dom事件綁定,監聽Enter鍵觸發        },        onInputKeypress: function (event) {            var ENTER_KEY = 13;            var todoText = this.ui.input.val().trim();            if (event.which === ENTER_KEY && todoText) {                this.collection.create({                    title: todoText                });                this.ui.input.val(‘‘);            }        }    });});

TodoMVC中的Backbone+MarionetteJS+RequireJS例子源碼分析之三 Views

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.