2015-04-27 69 views
7

我使用流星與AutoForm &鐵路路由器。使用鐵路路由器到Meteor autoform提交的新數據的路由?

我有一個autoform插入數據,我想重定向到成功插入後添加的數據頁面。我應該怎麼做?

這裏是爲了:

{{#autoForm collection="Products" id="add" type="insert"}} 
    <h4 class="ui dividing header">Products Information</h4> 
     {{> afQuickField name='name'}} 
     {{> afQuickField name='info'}} 
    <button type="submit" class="ui button">Insert</button> 
{{/autoForm}} 

鐵路由器:

Router.route('/products/:_id', { 
    name: 'page', 
    data: function() { return Products.findOne(this.params._id);} 
}); 

回調/鉤

AutoForm.hooks({ 
    add: { 
    onSuccess: function(doc) { 
     Router.go('page', ???); 
    } 
    } 
}); 
+0

的http://計算器.COM /問題/ 26 851878/meteor-ironrouter-passing-routers-goes- –

回答

6

了自動掛機將回到你的docId。參見: https://github.com/aldeed/meteor-autoform#callbackshooks

this.docId:連到形式DOC的_id屬性,如果 有一個,或對於類型=「插入」的形式中,新插入 文檔的_id,如果插入了一個。

所以使用:

Router.go('page',{_id: this.docId}); 
0

據GitHub上的文檔,簽名改爲: 不要忘了申報形式或空以應用掛鉤。

所有形式

AutoForm.addHooks(null,{ 
    onSuccess: function(formType, result) { 
     Router.go('page',{_id: this.docId}); 
    } 
}); 

具體形式

AutoForm.addHooks(['yourForm'],{ 
    onSuccess: function(formType, result) { 
     Router.go('page',{_id: this.docId}); 
    } 
}); 

最好是檢查最新的簽名:https://github.com/aldeed/meteor-autoform#callbackshooks

0
onSuccess: function(formType, result) { 
    Router.go(
     ['adminDashboard', result, 'Edit'].join(''), 
     {_id: this.docId} 
    ); 
}, 
+1

雖然這段代碼可能是解決方案,[包括解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-基於代碼的答案)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – xskxzr