我开始使用node.js和Chaplin
时需要一些帮助以下是该方案:
这是如何运作的?
通过尝试我已经学会了(因为我真的没有找到关于这个章节的任何好的指示......)是我需要创建区域,然后我将我的观点放入其中。这些视图只能包含此区域内的事件。我没有得到的是如何将这个“测试”div用作区域?
服务器
app.js
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
客户端
的index.html
<!doctype html>
<!--[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/stylesheets/app.css">
<script src="/javascripts/vendor.js"></script>
<script src="/javascripts/app.js"></script>
<script>require('initialize');</script>
</head>
<body>
<div id="test">
</div>
</div>
</body>
</html>
routes.coffee
module.exports = (match) ->
match '', 'home#index'
home-controller.coffee
Controller = require 'controllers/base/controller'
HeaderView = require 'views/home/header-view'
HomePageView = require 'views/home/home-page-view'
module.exports = class HomeController extends Controller
beforeAction: ->
super
@compose 'header', HeaderView, region: 'header'
index: ->
@view = new HomePageView region: 'main'
home-page-view.coffee
View = require 'views/base/view'
module.exports = class HomePageView extends View
autoRender: true
className: 'home-page'
template: require './templates/home'
events:
'click "testi"': 'testEvent'
testEvent: (event) ->
alert "Yeah"
在这个HomePageView中点击工作正常,但是如何将该事件绑定到我的html中的“test”div,或者我如何将现有的div用作区域?
答案 0 :(得分:0)
您可以在主布局视图中添加容器选项,其中值是您的dom元素,例如#测试。所以它可能看起来像这样。
View = require 'views/base/view'
module.exports = class HomePageView extends View
container: '#test'
autoRender: true
className: 'home-page'
template: require './templates/home'
events:
'click "testi"': 'testEvent'
testEvent: (event) ->
alert "Yeah"
我还建议你看看这个项目 - https://github.com/chaplinjs/chaplin-boilerplate。一个很好的样板示例,应该有助于您的理解。