到目前为止,我一直在使用backbone.js
进行大多数前端JavaScript项目,但在听到有关Facebook的react.js
后,我感兴趣并开始探索。
我想知道我是否应该再使用Backbone.View
类或用反应“组件”替换每个“视图” - 甚至是布局页面的“基本视图”。
我在仍然使用Backbone.View
s的互联网中发现了一些帖子 - 其他我偶然发现只创建了React-classes。
也许有人可以指出我正确的方向......在单页面应用程序中何时以及如何最好地实现多个“页面/状态”而不仅仅是通常(我可以忍受它)TODO示例。< / p>
这是我提出的一些代码:
Backbone init stuff:
require(
[
"jsx!app/view/base",
"react",
"app/router",
"backbone"
],
function (BaseView, React, Router, Backbone) {
"use strict";
var router = new Router();
var base = new BaseView({router: router});
React.renderComponent(base, document.getElementById("page"));
router.on("route", function(action) {
base.setProps({path: action});
});
Backbone.history.start({pushState: true});
}
);
app/router
:
define(function(require) {
"use strict";
var Backbone = require("backbone");
/**
*
*/
var Router = Backbone.Router.extend({
routes: {
"": "home",
"test": "test",
"*error": "404"
}
}); // end Router
return Router;
});
app/view/base.jsx
:
define(function(require) {
"use strict";
var React = require("react");
var mixins = require("app/utils/mixins");
var Header = require("jsx!app/view/header");
var ContentHome = require("jsx!app/view/content_home");
var ContentTest = require("jsx!app/view/content_test");
var ContentLogin = require("jsx!app/view/content_login");
/**
*
*/
var BaseView = React.createClass({
render: function() {
var content;
switch (this.props.path) {
case "home":
content = <ContentHome />
break;
case "test":
content = <ContentTest />
break;
case "login":
content = <ContentLogin />
break;
case "404":
default:
content = "Error, page not found";
break;
}
return (
<div id="base" onClick={this.onClick}>
<Header />
{content}
</div>
);
},
onClick: function(event) {
if (event.target.tagName.toLowerCase() === "a" &&
event.target.className === "main") {
event.preventDefault();
this.props.router.navigate(event.target.pathname, {trigger: true});
}
}
}); // end BaseView
return BaseView;
});
但我对此设置并不完全满意。欢迎任何意见。
答案 0 :(得分:2)
不要像你这样拥有对某个组件的引用。如果需要use refs或更好,只需在回调中呈现组件。 React将检查是否需要更新DOM。
require(
[
"jsx!app/view/base",
"react",
"backbone"
],
function (BaseView, React, Backbone) {
"use strict";
var router = Backbone.Router.extend({
routes: {
"": "home",
"test": "test",
"login": "login",
"*error": "404"
}
}).on('route', function(action) {
React.renderComponent(
<BaseView router={router} path={action} />,
document.getElementById("page")
);
});
Backbone.history.start({pushState: true});
}
);
编辑:
由于BaseView
中的逻辑与Router
中的逻辑非常相似,因此将它们保存在同一文件中是最有意义的。
var BaseView = React.createClass({
render: function() {
return (
<div id="base" onClick={this.onClick}>
<Header />
{this.props.children}
</div>
);
},
onClick: function(event) {
if (event.target.tagName.toLowerCase() !== "a") return;
if (event.target.className !== "main") return;
event.preventDefault();
this.props.router.navigate(event.target.pathname, {trigger: true});
}
});
app/router:
var router = Backbone.Router.extend({
routes: {
"": "home",
"test": "test",
"login": "login",
"*error": "404"
}
}).on('route', function(action) {
var pathMapping = {
"home": ContentHome,
"test": ContentTest,
"login": ContentLogin
};
var Content = pathMapping[action] || Content404;
React.renderComponent(
<BaseView router={router}>
<Content />
</BaseView>,
document.getElementById("page")
);
});
Backbone.history.start({pushState: true});
答案 1 :(得分:1)
var BaseView = React.createClass({
pathToComponent: function(){
var paths = {
'home': ContentHome,
'login': ContentLogin
};
return paths[path] || Content404
},
render: function() {
var ContentComponent = this.pathToComponent(this.props.path);
return (
<div id="base" onClick={this.onClick}>
<Header />
<ContentComponent />
</div>
);
},
}); // end BaseView
return BaseView;
});