Ractive.js routing
Ractive.js routing
所以我一直在将我的应用移植到reactive。我目前正在使用Swig从Express提供每个页面…在客户端呈现一个有吸引力的模板。当我可以提供一个页面并使用activex完成所有客户端渲染时,这似乎有点疯狂。
我知道react没有随路由器一起发货,并且确实在设计上遗漏了一个(为了提供灵活性等-这是有道理的)。我在谷歌上搜索了一下Stack overflow,看到了一些推荐的第三方路由器库…
然而,我找不到任何教程或建议的最佳实践,关于路由与叙事。我的问题是,有空位吗?
感谢** EDIT **
继martypdx的评论-这是我需要的路由:
/home <!-- a static page -->
/list <!-- a list of items -->
/list/:itemID <!-- a link to an items detail page -->
/contact <!-- a static contact page -->
在express我已经建立了一个简单的api来处理所有的db的东西。所有基本的CRUD内容。我用的是socket。
react .js和routing?这其实很简单,不需要魔法。
<!-- main.js -->
{{# route('/') }}<home />{{/}}
{{# route('/about') }}<about />{{/}}
{{# route('/404') }}<not-found />{{/}}
<script>
component.exports = {
data: {
route: function(path){
// Your pattern matching algorithm here. Return true if match.
// You can use location.pathname, location.search and location.hash
}
},
components: {
home: /* home constructor */,
about: /* about constructor */,
'not-found': /* not-found constructor */,
}
};
</script>
您还有其他选项,如计算属性:
{{# isHome }}<home />{{/}}
{{# isAbout }}<about />{{/}}
{{# isNotFound }}<not-found />{{/}}
<script>
function router(path){
return function(){
// Your pattern matching algorithm here. Return true if match.
// You can use location.pathname, location.search and location.hash
}
}
component.exports = {
computed: {
isHome: router('/'),
isAbout: router('/about'),
isNotFound: router('/404'),
},
components: {
home: /* home constructor */,
about: /* about constructor */,
'not-found': /* not-found constructor */,
}
};
</script>
至于传递数据,您也有很多选择。你可以使用oninit
,当组件被创建并准备渲染时运行(或者在这种情况下,当一个部分变得真实,即。当isHome
为true
时,为{{# isHome }}
)。以下是<home />
获取oninit
数据的示例:
<!-- home.js -->
<h1>Home</h1>
<div>{{someDynamicData}}</div>
<script>
var SomeDataStore = require('stores/some-data-store');
component.exports = {
oninit: function(){
// Let's say your data comes from an AJAX call
$.get(...).then(function(response){
this.set('someDynamicData', response);
}.bind(this));
// Or say it's from a listenable data store (like Reflux)
SomeDataStore.listen(function(){
this.set('someDynamicData', SomeDataStore.getSomeDynamicData());
});
}
}
</script>
或者你可以让路由组件获取并传递它(并且路由组件"拥有"该数据)。这与计算方法一起工作得很好,因为您可以观察计算值并在适当的视图出现时获取。
<!-- main.js -->
{{# isHome }}<home data="{{homeData}}" />{{/}}
{{# isAbout) }}<about data="{{aboutData}}" />{{/}}
{{# isNotFound }}<not-found data="{{notFoundData}}" />{{/}}
<script>
component.exports = {
...
oninit: function(){
this.observe('isHome', function(isHome){
if(!isHome) return;
// still the same here, you can use any data source, as long as you
// set to a data property in the end
this.get(...).then(function(response){
this.set('homeData', response);
}.bind(this));
});
this.observe('isAbout', function(isAbout){
if(!isAbout) return;
...
});
}
};
</script>
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 使用agility.js进行页面布局和合成
- 使用Clipboard.js复制span文本
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 强制模板刷新ember.js
- 如何编写HTML输入的JS内联
- Angular JS IE9 Hashbang url rewriting
- 使用JS将数组转换为json对象
- Node.js v6.2.0类扩展不是函数错误
- 当js函数's已执行
- 要求未定义JS回调参数
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- 如何从Java/scala调用js美化程序
- Routing from Javascript lib in Angular JS 2
- Angular JS & Node Webkit Routing
- Angular JS Routing with ng-click
- Backbone.js URL Routing
- Angular.js routing
- Ractive.js routing