-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathView.js
90 lines (80 loc) · 2.17 KB
/
View.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* ER (Enterprise RIA)
* Copyright 2013 Baidu Inc. All rights reserved.
*
* @file View类声明
* @author otakustay
*/
define(
function(require) {
var Observable = require('./Observable');
/**
* View类声明
*
* 在ER框架中,View不一定要继承该类,
* 任何有一个名为`render`的方法的对象均可作为View
*
* 该类结合`template`对象,实现了一个通用的RIA视图方案
*
* @constructor
* @extends Observable
*/
function View() {
Observable.apply(this, arguments);
}
/**
* 对应的模板
*
* @type {string}
* @public
*/
View.prototype.template = '';
/**
* 对应的Model对象
*
* @type {*}
* @public
*/
View.prototype.model = null;
/**
* 渲染容器的元素的id
*
* @type {string}
* @public
*/
View.prototype.container = '';
/**
* 渲染当前视图
*
* @public
*/
View.prototype.render = function() {
var container = document.getElementById(this.container);
var template = require('./template');
var model = this.model;
if (model && typeof model.get !== 'function') {
var Model = require('./Model');
model = new Model(model);
}
template.merge(container, this.template, model);
this.enterDocument();
};
/**
* 当容器渲染完毕后触发,用于控制元素可见性及绑定事件等DOM操作
*
* @protected
*/
View.prototype.enterDocument = require('./util').noop;
/**
* 销毁当前视图
*
* @public
*/
View.prototype.dispose = function() {
var container = document.getElementById(this.container);
container && (container.innerHTML = '');
};
require('./util').inherits(View, Observable);
return View;
}
);