如何获取骨干集合并传递给视图?

时间:2015-06-16 11:11:54

标签: javascript backbone.js

我在骨干采集方面遇到了问题。

这是我的路由器

@foreach ($subastas as $subasta)
    @if(($subasta->data_inici <= $data)&&($subasta->data_final>$data))
        <a href="item/{{$subasta->id}}"><div class="col-sm-4">
            <div class="product-image-wrapper">
                <div class="single-products">
                    <div class="productinfo text-center">
                        <div class="nombre">{{$subasta->nombre}}</div>
                        @foreach ($subasta->images as $word => $meaning)
                            @if($word==0)
                                <img src="{{ asset($meaning->ruta) }}" style="width="242px" height="164px""/>
                            @endif
                        @endforeach
                        <h2><span class="clock" data-countdown="{{ $subasta->data_final}}"></span></h2>
                        <p>{{$subasta->descripcion}}</p>
                        <p>{{$subasta->cant_actual}}</p>
                        <a class="pujar btn btn-warning" id="{{$subasta->id}}">Pujar</a>
                    </div>
                </div>
            </div>
        </div></a>
    @endif
@endforeach

收集

    var LanguageRouter = Backbone.Router.extend({
    routes: {
        '': 'defaultaction',
        'section/:key': 'sectionview',
    }
});

和app.js

 var LanguageCollection = Backbone.Collection.extend({
    model: LanguageModel,
    url: '/lang'
});

这里,在获取list_collection之后,我尝试将集合传递给language_view,但我只收到空集合。如何解决这个问题...提前致谢

1 个答案:

答案 0 :(得分:0)

提取不是同步的,因此您无法在一行中调用它并使用下一行的返回。使用骨干的正确方法是使用listenTo或On(取决于骨干版本)

您可以更改此部分:

list_collection = new LanguageCollection();
this.listenTo(list_collection, "sync", this.someFunction()); 
\\here we are listening the sync event that is automatically fired by backbone when a model or collection is synced with the server.

list_collection.fetch();


someFunction:function(){
    //Logic with the collection content...
}

要了解有关活动的更多信息,请在此处阅读:http://backbonejs.org/#Events-catalog