
JavaScriptMVC 函式庫
|
241
基本用法
檢視器的用法,不外乎都是把動態產生的模版安插進網頁某處。由於
jQuery.View
覆寫
了 jQuery 網頁元件編修函式,因此能讓檢視器的用法變得更簡單,就像這樣:
$("#foo").html('mytemplate.ejs',{message: 'hello world'})
前面這行程式做的事包括:
1. 載入
mytemplate.ejs
檔案裡的模版。假設模版的內容如下:
<h2><%= message %></h2>
2. 利用
{message: ‘hello world'}
產生新的模版內容:
<h2>hello world</h2>
3. 把剛產生好的模版內容,加進網頁元件
foo
裡:
<div id='foo'><h2>hello world</h2></div>
jQuery 網頁元件編修函式(Modifiers)
模版可以搭配下列 jQuery 網頁元件編修函式來使用:
$('#bar').after('temp.ejs',{});
$('#bar').append('temp.ejs',{});
$('#bar').before('temp.ejs',{});
$('#bar').html('temp.ejs',{});
$('#bar').prepend('temp.ejs',{});
$('#bar').replaceWith('temp.ejs',{});
$('#bar').text('temp.ejs',{}); ...