自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于Dojo實(shí)現(xiàn)MVC模式下的Ajax應(yīng)用

開發(fā) 前端
Dojo是一個(gè)用JavaScript語言實(shí)現(xiàn)的開源DHTML工具包。Dojo能夠讓你更容易使Web頁面具有動(dòng)態(tài)能力,或者在任何能夠穩(wěn)定支持JavaScript語言的環(huán)境中發(fā)揮作用。本文即是在Dojo基礎(chǔ)上實(shí)現(xiàn)MVC模式下的Ajax應(yīng)用。

本人要實(shí)現(xiàn)項(xiàng)目中的一項(xiàng)應(yīng)用是控制服務(wù)端返回來的音頻、文字在客戶端播放時(shí)的同步,相信都看到過baidu的歌曲試聽吧,聲文同步且支持拖放同步,此次實(shí)現(xiàn)多它一個(gè)功能,那就是點(diǎn)哪一句就播哪一句(當(dāng)然我不是為了播放歌曲).簡要說我在和服務(wù)器的交互中使用JSON(javascript object notation)傳輸數(shù)據(jù),服務(wù)端用Newtonsoft的.Net組件處理JSON數(shù)據(jù)序列化,至于具體的JSON格式那就你自己定義了,例如(最簡單的):

{ 
      Media : [{
      text : "......",
      start : "...",
            end : "...."
         },  ....]
         }

至于js下的MVC實(shí)現(xiàn),或許許多人這樣認(rèn)為“js僅僅是個(gè)腳本而已”,大概應(yīng)是Ajax的出現(xiàn)改觀了許多人對js的看法,其實(shí)用js可以寫出完全面向?qū)ο蟮某绦颍驗(yàn)閖s支持面向?qū)ο笳Z言的幾大重要特性,應(yīng)是一直以來大家所見到的js腳本給大家造成了不好的印象,js原本就是面向?qū)ο蟮恼Z言(我們見到許多由它寫成的結(jié)構(gòu)化的程序).看一下這篇文章,我的實(shí)現(xiàn)也是受它啟發(fā),延伸一點(diǎn)的就是引用Dojo的事件訂閱、發(fā)布機(jī)制.

說一下上述陳述功能的具體的實(shí)現(xiàn),在model方面實(shí)現(xiàn)首先實(shí)現(xiàn)一個(gè)容器型的model,解析JSON數(shù)據(jù)并擁有當(dāng)前句信息、所有句信息(數(shù)組)、設(shè)定當(dāng)前句方法:

ContainerModel:

dojo.lang.declare('ContainerModel',null,{
    initializer : function(jsonData)
    {
        var jsonObj=dojo.json.evalJson(jsonData);
        var sentences=new Array();
        for(var key in jsonObj.Sentences)
        {
            var sentenceObj=new SentenceModel(key,jsonObj.Sentences[key]);
            sentences.push(sentenceObj);
        }
        this._sentences=sentences;
        this._url=jsonObj.MediaUrl;
        this._selectedSentence = sentences[0]
    },
    
    getSentences : function () {
        return [].concat(this._sentences);
    },

    addItem : function (sentence) {
        this._sentences.push(sentence);
    },    

    setSelected : function (sentence) {
        this._selectedSentence = sentence;
    },
    
    reset : function (){
        this._selectedSentence = this._sentences[0];
    }
});

ItemModel:

dojo.lang.declare('ItemModel',null,{
    initializer : function(id,sentence)
    {
        this._id=id;
        this._jsonSentence=sentence;
       
        dojo.event.topic.subscribe("/PositionChange", this, this.invokeActive);
    },
   
    invokeActive : function(currentPos){
        //if curPos between this.startTime and this.endTime pulish:
        if(this._jsonSentence.StartTime<=currentPos && this._jsonSentence.EndTime>currentPos)
            dojo.event.topic.publish("/MeInvoked", this);
    },
   
    clickActive : function(){
        dojo.event.topic.publish("/MeClicked",this);
    }
});

另一個(gè)model代表上述的一句的信息,包含text、startTime、endTime,并且訂閱“/positionChange”事件(后面據(jù)mediaplayer定時(shí)發(fā)布),同時(shí)定義兩方法(此處會(huì)于View中用dojo.event的connect將其連于特定的用戶事件)用于發(fā)布當(dāng)前對象被激活的事件,于view中同時(shí)會(huì)為controller訂閱此對象激活所發(fā)布的事件,controller處理時(shí)會(huì)刷新container model的當(dāng)前項(xiàng)同時(shí)更新view的表現(xiàn)(如添加樣式),其中view對象除了為其他對象進(jìn)行一些事件連接、訂閱外,其render方法負(fù)責(zé)將container model的所有項(xiàng)render成特定的html元素(如span),其中決定model的顯示形式:

Viewer - Controller:

/**
 * a container view class to render on the webpage
 */
dojo.lang.declare('MainView',null,{
    initializer : function(model,controller,elements){
        this._model=model;
        this._controller=controller;
        this._elements=elements;
       
        dojo.event.topic.subscribe("/MeInvoked", this._controller, this._controller.proccessInvoke);
        dojo.event.topic.subscribe("/MeClicked", this._controller, this._controller.proccessClick);
    },
   
    render : function(){
        var div = this._elements.div;
        //remove children
        for(var i=0;i<div.childNodes.length;i++)
        {
            div.removeChild(div.childNodes[i]);
        }
        div.innerHTML="";
        div.innerText="";
       
        var items = this._model.getSentences();
        for (var key in items) {
            var span = document.createElement("span");
            span.id=items[key]._id;
            span.appendChild(document.createTextNode(items[key]._jsonSentence.Sentence));
            span.appendChild(document.createElement("br"));
            div.appendChild(span);
           
            if(key==0)
                dojo.html.addClass(document.getElementById(this._model._selectedSentence._id),"selected");
           
            dojo.event.connect(span, 'onclick', items[key], 'clickActive');
        }
    }
   
});

/**
 * a common controller class,
 * execute some utilities operations.
 */
dojo.lang.declare('MainController',null,{
    initializer : function(model){
        this._model=model;
    },
   
    displaySentence : function(){       
        //actual method
        dojo.event.topic.publish("/DisplaySentence",this._model._selectedSentence._jsonSentence);
    },
   
    proccessInvoke : function(sentence){
        //proccess details
        this.proccessRightShow(sentence);       
    },
   
    proccessClick : function(sentence){
        //proccess details
        this.proccessRightShow(sentence);       
        //set player pos(start,end)
        setPlayerPos(sentence._jsonSentence.StartTime);
    },
   
    proccessRightShow : function(sentence){
        //lighten sentence and show sentence on the right
       
        if(this._model._sentences[0]==sentence || this._model._selectedSentence!=sentence)
        {
            //change origin selectedSentence's css
            dojo.html.removeClass(document.getElementById(this._model._selectedSentence._id),"selected");
            this._model.setSelected(sentence);
            //change new current selectedSentence's css
            dojo.html.addClass(document.getElementById(this._model._selectedSentence._id),"selected");
            document.getElementById(parseInt(this._model._selectedSentence._id/1.2)).scrollIntoView(true);
            //pass sentence to show in right in another func
            this.displaySentence();
        }
    }
});

大概模式如下:

圖中對象初始化會(huì)subscribe合適的事件以待事件publish時(shí)進(jìn)行處理,其中虛線表示一次用戶點(diǎn)擊處理,而自由線表示隨播放進(jìn)行處理文本同步(如加亮當(dāng)前項(xiàng))的過程,此過程在播放過程中持續(xù)進(jìn)行。其實(shí),事件發(fā)布并非圖中所示指向特定對象(圖中為了容易理解),是誰訂閱誰處理,有AOP的意味!

相信有了這些,讓這個(gè)模型運(yùn)行起來是沒問題了吧,忙中抽閑和大家分享,另外dojo的require不要忘了

dojo.require('dojo.lang.*');
dojo.require("dojo.event.*");
dojo.require("dojo.event.topic");
dojo.require("dojo.html.*");
dojo.require("dojo.json");
dojo.require("dojo.io.*");

腳本的開發(fā)還是比較困難的,從開發(fā)環(huán)境、或從其控制來講,正如Pragmatic Programmer中所說的,“不***的系統(tǒng)、荒謬的時(shí)間標(biāo)度、可笑的工具、還有不可能實(shí)現(xiàn)的需求--在這樣一個(gè)世界上,讓我們安全‘駕駛’”!

【編輯推薦】

  1. AJAX和XmlHttpRequest下的Web開發(fā)
  2. 淺談Ajax在ASP.Net中的使用
  3. 使用AJAX擴(kuò)展器自定義控件
責(zé)任編輯:楊鵬飛 來源: 博客園
相關(guān)推薦

2009-03-09 09:45:07

MVCAjax.Net

2009-09-22 12:22:54

ibmdwLotus

2009-01-03 14:39:04

ibmdwDojoMVC

2011-01-24 13:12:01

AjaxDojojavascript

2009-01-03 16:29:45

AJAXASP.NET.NET

2017-11-23 17:21:31

Yii框架IntelYii框架深度剖析

2009-11-24 14:22:03

基于PHP的AJAX技

2012-09-28 10:18:53

IBMdw

2012-11-12 10:34:50

IBMdw

2011-08-01 16:43:51

ibmdwHTML5Dojo

2012-08-13 10:23:33

IBMdW

2012-12-18 10:03:22

JavaScriptWebJS

2012-12-18 13:32:45

IBMdW

2009-07-30 13:45:40

ASP.NET開發(fā)模式MVC模式

2009-06-01 09:13:52

ASP.NET MVCMVC應(yīng)用ASP.NET MVC

2011-09-08 09:38:46

HTML5 WidgeDojo

2009-03-31 13:12:05

ASP.NETMVC表單驗(yàn)證

2011-05-18 13:28:46

jQueryPHPAJAX

2017-11-22 14:08:23

OVSVLAN虛擬化網(wǎng)

2009-07-22 18:07:55

論壇應(yīng)用程序ASP.NET MVC
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號