我的用例是我有一个Node应用程序,它使用来自CMS的数据,在那个CMS中我给用户提供了选择React Component作为"Layout"的能力.我希望Relay能够从动态选择组件中获取GraphQL片段.当布局的父组件安装时,它会遍历其查询并获取所需的布局组件并设置一个Relay变量 - 然后需要从该组件中获取一个片段.有没有办法做到这一点?
这是父级查询:
export default Relay.createContainer(WordpressPage, {
initialVariables:{
Component: null,
page: null,
showPosts: false,
limit: 5
},
prepareVariables(prevVars){
return{
...prevVars,
showPosts: true
}
},
fragments: {
viewer: ({Component, showPosts, limit}) => Relay.QL`
fragment on User {
${PostList.getFragment("viewer", {limit:limit}).if(showPosts)},
page(post_name:$page){
id,
post_title,
post_type,
post_content,
thumbnail,
layout{
meta_value
}
}
}
`,
},
});
Run Code Online (Sandbox Code Playgroud)
如您所见,它查询并获取布局字段.安装时,它将Relay Component变量设置为React Component.而不是"PostList.getFragment",我真的希望能够做一个Component.getFragment.
我对Meteor.js很新,我发现文档有点难以理解.
我从一个非常简单的应用程序开始,用户可以通过单击按钮简单地允许用户将现有游戏添加到他们的个人资料中.该游戏存储在另一个流星集合.
在rails中,我只想创建一个has_and_belongs_to_many关系,但这不是Meteor的工作方式.我认为最好的方法是在创建用户帐户时添加一个空数组 - 然后,当他们点击"添加游戏"按钮时,它会将游戏的标题传递给用户数组.
我在/server/users.js文件中有这个:
Accounts.onCreateUser(function(options, user){
user.games = [];
return user;
});
Meteor.methods({
addGame: function(title) {
Meteor.users.update(Meteor.userId(), { $addToSet: { games: title}});
}
});
Run Code Online (Sandbox Code Playgroud)
我正在调用addGame我的/client/views/games/games_list.js文件中的方法:
Template.gamesList.events({
'click .add-to-chest-btn': function(e){
var title = $(e.target).attr('name');
e.preventDefault();
Meteor.call('addGame', title, function(title){ console.log(title)});
}
});
Run Code Online (Sandbox Code Playgroud)
我是在正确的轨道上还是有更好的方法来做到这一点?