Backbone.js的 - 的基本路径与子路径Concat的 [英] Backbone.js - base path not concat with subpaths

查看:348
本文介绍了Backbone.js的 - 的基本路径与子路径Concat的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的基地集合我有基本路径,从基本路径我正在延长进一步的网址..但我同时在扩展集合中控制台的URL我没有得到的URL的完整路径。

而不是只是我收到的网址 - 什么扩展的收集有..为什么我收到像这样,什么应该是正确的做法。

这是我尝试:

  BaseCollection = Backbone.Collection.extend({
    路径:HTTP://路径/要/ API
});TeachersCollection = BaseCollection.extend({
    网址:XYZ / ABC,
    初始化:功能(){
        的console.log(this.url); // XYZ / ABC - 为什么我得到我喜欢这个,而不是完整的路径?
        //完整的URL应该是'HTTP://路径/要/ API / XYZ / ABC' - 我怎样才能得到这样的..?
    }
});变种x =新TeachersCollection;

现场演示


解决方案

  1. 路径不在任何骨干班的一个特殊属性

  2. 模型可以有 urlRoot ,但有没有这样的事情对集合

这里是一个为你工作的方法的:

  TeachersCollection = BaseCollection.extend({
    网址:函数(){
        返回this.path +/ XYZ / ABC
    },
    初始化:功能(){
        // this.url = _.result(这一点,URL);
        的console.log(_结果(这一点,网址));
    }
});

您可能实际上要考虑改变构造上你的基地集合,这样如果您再会被延长了很多:

  BaseCollection = Backbone.Collection.extend({
    构造:函数(){
        this.url =的http://路径/要/ API'+ this.url;
        Backbone.Collection.prototype.constructor.apply(这一点,参数);
    }
});TeachersCollection = BaseCollection.extend({
    网址:/ XYZ / ABC,
    初始化:功能(){
        的console.log(this.url); // XYZ / ABC
        //完整的URL应该是'HTTP://路径/要/ API / XYZ / ABC
    }
});变种x =新TeachersCollection;

In my base collection i have the base path, from the base path i am extending further urls.. but while i console the url in the extended collection i am not getting the full path of the url..

instead just i am getting the url - what the extended collection have.. why i am getting like so, and what should be the proper approach?

here is my try :

BaseCollection = Backbone.Collection.extend({
    path: 'http://path/to/api'
});

TeachersCollection = BaseCollection.extend({
    url:"xyz/abc",
    initialize:function(){
        console.log(this.url);//xyz/abc - why i am getting like this instead of full path?
        //full url should be 'http://path/to/api/xyz/abc' - how can i get like this..?
    }
});

var x = new TeachersCollection;

live demo

解决方案

  1. path is not a special property on any of Backbone's classes
  2. Models can have urlRoot, but there's no such thing for collections

Here's an approach that should work for you:

TeachersCollection = BaseCollection.extend({
    url:function() {
        return this.path + "/xyz/abc"
    },
    initialize:function(){
        // this.url = _.result(this, 'url');
        console.log(_.result(this, 'url'));
    }
});

You may actually want to think about changing the constructor on your base collection, like this if you're going to be extending it a lot:

BaseCollection = Backbone.Collection.extend({
    constructor: function() {
        this.url = 'http://path/to/api' + this.url;
        Backbone.Collection.prototype.constructor.apply(this, arguments);
    }
});

TeachersCollection = BaseCollection.extend({
    url: "/xyz/abc",
    initialize:function(){
        console.log(this.url);//xyz/abc
        //full url should be 'http://path/to/api/xyz/abc'
    }
});

var x = new TeachersCollection;

这篇关于Backbone.js的 - 的基本路径与子路径Concat的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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