我一直在使用Meteor + Iron Router来处理我正在研究的多页面应用程序,而且我已经陷入了关于命名率的辅助函数.具体来说,我一直试图让我的导航栏选项卡的活动类更新每个路由更改.
以下是我项目的相关代码:
Router.configure({
layoutTemplate: 'mothership',
yieldTemplates: {
'header' : {to: 'header'},
'footer': {to: 'footer'}
},
});
Router.map(function () {
// Home page
this.route('home', {
path: '/',
template: 'home',
});
this.route('about', {
path: '/about',
template: 'about',
});
this.route('emails', {
path: '/emails',
template: 'emails',
});
this.route('people', {
path: '/people',
template: 'people',
});
});
Run Code Online (Sandbox Code Playgroud)
<template name="mothership">
<a href="#content" class="sr-only">Skip to content</a>
<div id="wrap">
<!-- header -->
<div>{{yield 'header'}}</div>
<div id="content">{{yield}}</div>
</div>
<div id="push"></div>
<div id="footer">
{{yield 'footer'}}
</div> …Run Code Online (Sandbox Code Playgroud) 使用流星铁路由器我怎样才能既呈现数据作为JSON或简单地显示它的"原始"(没有布局模板)
基本上我希望能够做到这样的事情:
this.route('rawdata', {
path: '/raw/:collection',
layoutTemplate: 'nolayout',
template: 'raw'
})
Run Code Online (Sandbox Code Playgroud)
where / raw/posts将显示Posts(集合)的原始json.
谢谢!
备注:
我知道Meteor中的JSON端点 但是流星路由器已经停止,而Iron-Router似乎没有JSON端点功能.
我也看了一下https://atmospherejs.com/package/collection-api,但它不适合我的需要,因为我需要能够选择集合/记录的字段子集.
我有两个型号:sites和articles.每个站点可以有多个文章.我想在这条路线中查看这篇文章:/siteName/articleFriendlyTitleUrl.目前我有帮助方法来制作这样友好的网址:
getFriendlyUrl = function(urlString){
urlString = urlString.toLowerCase();
urlString = str.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '-');
return urlString;
}
Run Code Online (Sandbox Code Playgroud)
我的router.js档案:
this.route('showArticle', {
path: '/article/:title'),
layoutTemplate: 'artLayout',
data: function(){
viewData = {
title: Articles.findOne({title: this.params.title}),
site: Sites.findOne({name: this.name})
}
return viewData;
});
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何实现这个?我也试过这个路径:
/:siteName/:articleId
Run Code Online (Sandbox Code Playgroud)
但没有成功 - 任何建议?
我想在旋转木马里面显示一些帖子.对于旋转木马,我使用OwlCarousel.
<div class="owl-carousel" id="featured-carousel">
{{#each featuredPosts}}
<div>
<h2>
{{postTitle}}
</h2>
</div>
{{/each}}
</div>
Run Code Online (Sandbox Code Playgroud)
我这样叫我的旋转木马:
Template.featuredCarousel.rendered = function(){
$('#featured-carousel').owlCarousel({
loop:true,
autoplay:true,
autoplayTimeout:3000,
items:1,
smartSpeed:1080,
padding:80
});
this.rendered = true;
};
Run Code Online (Sandbox Code Playgroud)
我得到的结果是Owl基本上认为我只有一个项目要在转盘中显示多个div.显然,在模板的#each-part完成之前或数据到达之前调用Template.featuredCarousel.rendered中的函数.
如何在完全呈现模板(包括所有数据)后调用实例化轮播的功能?
非常感谢您的帮助.
PS:我使用铁路由器进行路由,如下所示:
Router.map(function(){
this.route('home', {
path: '/',
waitOn: function(){
return Meteor.subscribe('featured');
},
data: function(){
return {featuredPosts: Featured.find({})};
}
});
});
Run Code Online (Sandbox Code Playgroud)
PPS:我也尝试过使用加载模板,但这也无济于事.
流行的帐户入口包中有一个与铁路由器相关的错误.我相信铁路由器的后期版本改为更好地作为中间件工作,因此呼吁Router.routes
在该文件的第87行,使用以下代码:
_.each Router.routes, (route)->
exclusions.push route.name
# Change the fromWhere session variable when you leave a path
Router.onStop ->
# If the route is an entry route, no need to save it
if (!_.contains(exclusions, Router.current().route?.getName()))
Session.set('fromWhere', Router.current().path)
Run Code Online (Sandbox Code Playgroud)
不幸的是,似乎没有在Router.rou上做一个_.each是一个解决方案,因为Router.routes不会返回并且对象中包含.name属性.
你如何获得最新铁路由器的所有路线的名称?
编辑:我使用的解决方案是@Kyll的解决方案.
假设我想要返回的服务器端对象"构建起来很复杂",需要来自不同集合的不同属性.
我第一次尝试:
/server/publications.js
Meteor.publish('myCustomDocument', function(){
// suppose here that I need to X.find() different collections
// and create a complex Array of JSON data (which contains different
// attributes from different Collections
return [
{appName: 'aName',
category: 'catName',
anotherField: 'something'},
(...)
];
});
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为它没有返回游标.我想要做的是创建一个从不同集合构建的文档(或文档数组).
我不需要观察该文档的更改.
我为它创建了一个集合:
/collections/myCollection.js
MyCollection = new Meteor.Collection('myCollection');
Run Code Online (Sandbox Code Playgroud)
在客户端,使用铁路由器,我试图做的是:
/lib/router.js
this.route('myPage',{
path: '/myPage',
waitOn: function(){ return Meteor.subscribe('myCollection'); },
data: function(){ return MyCollection.find(); }
});
Run Code Online (Sandbox Code Playgroud)
如何实现向客户端发送非活动数据?
我正在使用铁路由器,我有一个路线"模型",其中有一个项目列表.当用户单击其中一个项目时,将使用新的路径"模型",所选项目的名称将作为参数传递,并用于从数据库加载有关该模型的数据.
我想使用光滑的轮播,使用从数据库返回的图像数组(基于参数).
<div id="carousel">
{{#each images}}
<div class="item">
<img src="/images/{{this}}" alt="">
</div>
{{/each}}
</div>
Run Code Online (Sandbox Code Playgroud)
我应该在哪里调用光滑的init?
使用Meteor 1.2.0.1和React.我的简单app很棒,但现在我需要铁路由器.
应用布局:
client\
app.jsx
lib\
router.jsx
server
views\
home.jsx
layout.jsx
Run Code Online (Sandbox Code Playgroud)
home.jsx:
Home = React.createClass({
render() {
return (
<div>
<h3>Hello World</h3>
<p>from home</p>
</div>
);
}
});
Run Code Online (Sandbox Code Playgroud)
layout.jsx:
Layout = React.createClass({
render() {
return (
<div>
{this.props.children}
</div>
);
}
});
Run Code Online (Sandbox Code Playgroud)
routes.jsx:
Router.route('/', () => {
let page = (
<Layout>
<Home/>
</Layout>
);
React.render(page, document.body);
});
Run Code Online (Sandbox Code Playgroud)
当然,在我的app.jsx工作中,它可以很好地显示在html的主体上,但是设置不适用于多页应用程序,因此需要路由.里面是:
Meteor.startup(() => {
let app = (
<Layout>
<Home/>
</Layout>
);
React.render(app, document.body);
});
Run Code Online (Sandbox Code Playgroud)
问题是,如何让铁路由器(routes.jsx)显示内容?
我的应用程序一直在工作,直到我将其更新为Meteor 1.3.现在我收到这个错误:
Exception in callback of async function: Error: Handler with name 'onBeforeAction' already exists.
at MiddlewareStack._create (http://localhost:3000/packages/iron_middleware-stack.js?hash=8a2aa73e86a32698fb9f60cea452e0ecb2e72b7f:190:13)
at MiddlewareStack.push (http://localhost:3000/packages/iron_middleware-stack.js?hash=8a2aa73e86a32698fb9f60cea452e0ecb2e72b7f:206:22)
at http://localhost:3000/packages/iron_middleware-stack.js?hash=8a2aa73e86a32698fb9f60cea452e0ecb2e72b7f:224:12
at Array.forEach (native)
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?hash=8de51f9d86e95ae2ffee15a8db324a1decccba3e:139:11)
at MiddlewareStack.append (http://localhost:3000/packages/iron_middleware-stack.js?hash=8a2aa73e86a32698fb9f60cea452e0ecb2e72b7f:220:5)
at http://localhost:3000/packages/iron_middleware-stack.js?hash=8a2aa73e86a32698fb9f60cea452e0ecb2e72b7f:226:19
at Array.forEach (native)
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?hash=8de51f9d86e95ae2ffee15a8db324a1decccba3e:139:11)
at MiddlewareStack.append (http://localhost:3000/packages/iron_middleware-stack.js?hash=8a2aa73e86a32698fb9f60cea452e0ecb2e72b7f:220:5)
Run Code Online (Sandbox Code Playgroud)
我使用的唯一地方onBeforeAction是我的铁路由器路由控制器.它们都在同一个router.js文件中.
我定义了几个路由控制器,首先ApplicationController是扩展RouteController,然后是各种扩展的控制器ApplicationController.
ApplicationController = RouteController.extend({
onBeforeAction: function() {
//some code
this.next();
}
});
SomeController = ApplicationController.extend({
onBeforeAction: function() {
//some code
this.next();
}
});
Run Code Online (Sandbox Code Playgroud)
从错误消息我无法找出错误或在哪里寻找问题.
错误消息中提到的包的版本是:
iron:router@1.0.12 …Run Code Online (Sandbox Code Playgroud) iron-router ×10
meteor ×10
routing ×3
json ×1
meteor-blaze ×1
middleware ×1
owl-carousel ×1
reactjs ×1
slick.js ×1
templates ×1
updates ×1