
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];
}
});

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);
}
});另一个model代表上述的一句的信息,包含text、startTime、endTime,并且订阅“/positionChange”事件(后面据mediaplayer定时发布),同时定义两方法(此处会于View中用dojo.event的connect将其连于特定的用户事件)用于发布当前对象被激活的事件,于view中同时会为controller订阅此对象激活所发布的事件,controller处理时会刷新container model的当前项同时更新view的表现(如添加样式),其中view对象除了为其他对象进行一些事件连接、订阅外,其render方法负责将container model的所有项render成特定的html元素(如span),其中决定model的显示形式,

/**//**
* 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.getElementB 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-434691/ ,如需转载,请注明出处,否则将追究法律责任。