BackboneJS + Codeigniter RESTful API - 如何获取包含变量的API

时间:2014-02-21 00:06:33

标签: api codeigniter backbone.js

我有这个Backbone App我想要获取数据,具体取决于我从Artist获得的ID。在这种情况下,我希望获取传记。 Restful API使用Codeigniter构建。现在我没有收到任何错误,只是没有数据,有没有人知道如何获取包含Backbone变量的API?

public function artist_bio_get()  
{ 
    $this->load->database();
    $sql = "SELECT artist_bio.bio FROM artist_bio INNER JOIN artists ON artists.artist_id = artist_bio.artist_id WHERE artist_bio.artist_id = '$artist_id'";
    $query = $this->db->query($sql);
    $data = $query->result();

    if($data) {
        $this->response($data, 200); // 200 being the HTTP response code
    } else {
        $this->response(array('error' => 'Couldn\'t find any artist biography!'), 404);
    }
}

My Backbone View看起来像这样:

function (App, Backbone) {

    var Artistbio = App.module();

    Artistbio.View = Backbone.View.extend({
        className: 'artistbio',
        template: 'artistbio',
        initialize: function() {
            this.listenTo(this.collection, 'all', this.render)
        },
        serialize: function() {
            return this.collection ? this.collection.toJSON() : [];
        }
    });
    Artistbio.ArtistbioCollection = Backbone.Collection.extend({
        url: function() {
            return '/projects/mdk/index.php/api/artistchannel/artist_bio';
        }
    });;

    return Artistbio;
}

[[UPDATE]]

好吧我改变了我的观点:

Artistbio.ArtistbioCollection = Backbone.Collection.extend({
        url: function() {
            return '/projects/mdk/index.php/api/artistchannel/artist_bio/' + this.artist_id;
        }
});

然后,在我的MainView中,我做了:

var artistbioCollection = new Artistbio.ArtistbioCollection();
artistbioCollection.artist_id = this.artist_id;
this.insertView(new Artistbio.View({collection: artistbioCollection}));
artistbioCollection.fetch();

然后,我做了一个像这样的控制器:

ArtistController.prototype.initArtist = function(letter, id) {
     App.trigger('navigate', 'artistchannel');
     this.artistView.artist_id = id;
     App.useLayout('artistchannel', 'artistchannel').setViews({
        '.artistsDiv': this.artistView
     }).render();
};

当我在MySQL查询中输入ID时,如WHERE artist_bio.artist_id = '86'"; - 我得到了正确的结果,但是当我保留$artist_id时,它会给我错误 Undefined variable: artist_id

这可能是什么问题?

[[更新2]]

我制作了一个像这样的控制器:

define(function (require, exports, module) {
  var ArtistModule = require('modules/artist');
  var ArtistController = function(navigation) {
     this.navigation = navigation;
     this.artistView =  new ArtistModule.View();
 };

然后是我的artistview-module

define([

'app',
'backbone',
'modules/artistBio',
'modules/artistRelated',
'modules/artistAlbums'
],

function (App, Backbone, Artistbio, ArtistRelated, ArtistAlbums) {

    var Artist = App.module();

    Artist.View = Backbone.View.extend({
        className: 'artist',
        beforeRender: function() {
            var artistbioCollection = new Artistbio.ArtistbioCollection();
            artistbioCollection.artist_id = this.artist_id;
            this.insertView(new Artistbio.View({collection: artistbioCollection}));
            artistbioCollection.fetch();

            var artistalbumsCollection = new ArtistAlbums.ArtistAlbumsCollection();
            artistalbumsCollection.artist_id = this.artist_id;
            this.insertView(new ArtistAlbums.View({collection: artistalbumsCollection}));
            artistalbumsCollection.fetch();             

            var artistrelatedCollection = new ArtistRelated.ArtistRelatedCollection();
            artistrelatedCollection.artist_id = this.artist_id;
            this.insertView(new ArtistRelated.View({collection: artistrelatedCollection}));
            artistrelatedCollection.fetch();
        },
    });

    return Artist;
});

我的Codeigniter REST_Controller查询可能有问题吗?

1 个答案:

答案 0 :(得分:0)

你必须将artistId放到某处并将其传递给集合url函数:

url: function() {
    return '/projects/mdk/index.php/api/artistchannel/artist_bio/' +
    /* something like this.options.artistId */;
}

通过此this.artistView.artist_id = id;

更改此this.artistView.collection.artist_id = id;