【问题标题】:BackboneJS + Codeigniter RESTful API - How to fetch API containing variablesBackboneJS + Codeigniter RESTful API - 如何获取包含变量的 API
【发布时间】:2014-02-21 00:06:32
【问题描述】:

我有这个 Backbone 应用程序,我想在其中获取数据,具体取决于我从艺术家那里获得的 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);
    }
}

我的主干视图如下所示:

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;
}

[[更新]]

好的,我这样改变了我的视图:

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();
 };

然后是我的艺术家视图模块

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 查询可能有问题吗??

【问题讨论】:

    标签: api codeigniter backbone.js


    【解决方案1】:

    您必须在某处获取artistId 并将其传递给collection 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;

    【讨论】:

    • 是的,我收到了return '/projects/mdk/index.php/api/artistchannel/artist_bio/' + this.artistId; - 但是现在,我收到关于Undefined variable: artist_id 的 PHP 错误??
    • artistId 参数传递到哪里了?
    • 这给了我Uncaught TypeError: Cannot set property 'artist_id' of undefined
    • 你在哪里声明 this.artistView ?你能创建一个 jsfiddle 吗?
    猜你喜欢
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    相关资源
    最近更新 更多