一、bower: 專門爲前端表現設計的程序包管理器,相似npm,主要用於前端優化。bower能夠管理包含HTML,CSS,JavaScript,字體甚至是圖像文件的組成部分。bower並無作別的事 - 它僅僅安裝你須要的包及其依賴的正確版本。javascript
二、**依賴node環境,window用戶必須安裝git工具。安裝和升級bower必須用npm。css
三、npm 和bower 的最大區別,就是 npm 支持嵌套地依賴管理(),而 bower只能支持扁平的依賴(嵌套的依賴,由程序員本身解決)。html
**npm與bower的區別。前端
1.npm不能共享多個包共同依賴的包,每一個包都會下載本身依賴的包。bower對於共享包只會下載一次。java
2. npm主要運用於npm.js項目的內部依賴包管理,安裝的模塊位於項目根目錄下的node_modules文件夾內。而bower大部分狀況下用於前端開發,對於CSS/JS/模板等內容進行依賴管理,依賴的下載目錄結構能夠自定義。node
實際項目中,能夠採用npm做用於後端;bower做用於前端的組合使用模式jquery
四、bower提供清單文件bower.json
跟蹤程序包,便於查閱包的版本、依賴狀況。bower提供api和編程api,可在git bash、cmd或js中對包進行安裝、卸載、更新、刪除、發佈等操做。git
五、bower提供的api程序員
bower install jquery:安裝jquery庫。github
bower cache clean:安裝失敗後,清除緩存
六、bower提供的程序api---提供了能夠操做的bower. commands
對象。
var bower = require('bower');
bower.commands
.install(['jquery'], { save: true }, { /* custom config */ })
.on('end', function (installed) {
console.log(installed);
});
bower.commands
.search('jquery', {})
.on('end', function (results) {
console.log(results);
});
七、bower中配置文件.bowerrc
{
"directory" : "components",
"json" : "bower.json",
"endpoint" : "https://Bower.herokuapp.com",
"searchpath" : "",
"shorthand_resolver" : ""
}
八、handlebars包的使用。
html代碼:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Handlebars demo</title>
<script type="text/javascript" src="../bower_components/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="../bower_components/handlebars/handlebars.min.js"></script>
<script type="text/javascript" src="../javascript/myjs.js"></script>
<style type="text/css"></style>
</head>
<body>
<h2>Simple handlebars demo</h2>
<button id="btn_simple">Click me</button>
<div id="my_div">
</div>
<h2>Handlebars Helpers demo</h2>
<button id="btn_helper">Click me</button>
<div id="helper_div">
</div>
<script id="some-template" type="text/x-handlebars-template">
<table>
<thead>
<tr>
<th>Username</th>
<th>Real Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
{{#if users}}
<tr>
<td>{{username}}</td>
<td>{{firstName}} {{lastName}}</td>
<td>{{email}}</td>
</tr>
{{else}}
<tr>
<td colspan="3">NO users!</td>
</tr>
{{/if}}
</tbody>
</table>
</script>
<script id="helper-template" type="text/x-handlebars-template">
<div>
<h1>By {{fullName author}}</h1>
<div>{{body}}</div>
<h1>Comments</h1>
{{#each comments}}
<h2>By {{fullName author}}</h2>
<div>{{body}}</div>
{{/each}}
</div>
</script>
</body>
</html>
js代碼:
$(document).ready(function () {
Handlebars.registerHelper('fullName', function (person) {
return person.firstName + " " + person.lastName;
});
$("#btn_simple").click(function () {
// $(this).hide();
showTemplate();
});
$("#btn_helper").click(function () {
showHowUseHelper();
});
});
// var context = {title: "My New Post", body: "This is my first post!"};
var persion = { title: "My New Post", body: "This is my first post!" }
function showTemplate() {
var source = $("#some-template").html();
var template = Handlebars.compile(source);
var data = {
users: [
{ username: "alan", firstName: "Alan", lastName: "Johnson", email: "alan@test.com" },
{ username: "allison", firstName: "Allison", lastName: "House", email: "allison@test.com" },
{ username: "ryan", firstName: "Ryan", lastName: "Carson", email: "ryan@test.com" }
]
};
$("#my_div").html(template(data));;
}
function showHowUseHelper() {
var context = {
author: { firstName: "Alan", lastName: "Johnson" },
body: "I Love Handlebars",
comments: [{
author: { firstName: "Yehuda", lastName: "Katz" },
body: "Me too!"
}]
};
var source = $("#helper-template").html();
var template = Handlebars.compile(source);
$("#helper_div").html(template(context));;
}