Template.tmpl.helpers()中Iron-Router RouteController中的调用方法不起作用 [英] call method in Iron-Router RouteController from Template.tmpl.helpers() not working

查看:62
本文介绍了Template.tmpl.helpers()中Iron-Router RouteController中的调用方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次尝试调用loadMoreClients方法时,我都尝试将新数据推送到我的clients数组中.该出版物期望此back_to参数,并且知道如何处理它.我的问题是我似乎无法从模板助手中调用这些方法.

I'm trying to push new data on to my clients array each time the loadMoreClients method is called. The publication is expecting this back_to parameter and knows how to handle it. My problem is that I can't seem to call these methods from my Template helpers .

我在控制台上登录了IronIron.controller,这两个都存在并且向我展示了我期望看到的内容.我似乎找不到当前的文档或如何从模板助手中访问Iron.controller()方法/属性的示例

I logged Iron and Iron.controller to the console and both of those exist and are showing me what I expected to see. I just can't seem to find current docs or examples of how to access Iron.controller() methods/properties from my Template helpers

这是我的RouteController代码:

Here is my RouteController code:

ClientController = ApplicationController.extend({
action : function(){
    this.render(Router.current().route.getName())
},

data : function(){
    if( this.params._id ){
        return Clients.findOne({ _id:this.params._id })
    }
},

waitOn : function(){
    return [
        Meteor.subscribe('directory'),
        Meteor.subscribe('clients')
    ]
},

loadMoreClients : function(){
    this.months_back += 3
    this.back_to = moment().subtract(this.months_back,'months').startOf('day')
    this.clients.push(Meteor.subscribe('clients', {back_to:this.back_to, skip:this.clients.length}))
},

loadAllClients : function(){
    this.clients.push(Meteor.subscribe('clients', {back_to:this.start_of_time, skip:this.clients.length}))
},

// we'll use these properties to 'load more' client data
clients : [],
back_to : moment().subtract(3,'months').startOf('day'),
months_back : 3,
start_of_time : moment(new Date(0))
})

这是我的助手代码:

Template.client_list.helpers({
clients : function(){
    var clients = []
    Iron.controller().clients.forEach(function(client){
        // ... some stuff here...
        clients.push(client)
    })

    return clients
},

earliestClientLoaded : function(){
    var controller = Iron.controller()
    return controller.clients[controller.clients.length - 1].createdAt
}
})

Template.client_list.events({
'click .btn-load-more' : function(e){
    e.preventDefault()

    Iron.controller().loadMoreClients()
},

'click .btn-load-all' : function(e){
    e.preventDefault()

    Iron.controller().loadAllClients()
}
})

Iron.controller()loadAllClients方法的Iron.controller()调用中出现undefined function错误.

I'm getting undefined function errors on my Iron.controller() calls to loadMoreClients and loadAllClients methods.

我在这里做什么错了?

推荐答案

我已通过简单地更新订阅以订阅早于当前加载日期的帖子来更改此问题的方法.

I have changed the approach to this problem by simply updating the subscription to subscribe to posts earlier than the currently loaded date.

它正在工作,尽管当我想加载更多内容时,似乎应该有一种比必须订阅waitOn更好的方法.

It is working, although seems like there should be a better way than having to waitOn the subscription when I want to load more.

这篇关于Template.tmpl.helpers()中Iron-Router RouteController中的调用方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆