即使正确设置了modobj.attributes,Backbone js model get方法也会返回“(空字符串)”

时间:2013-08-23 19:28:18

标签: codeigniter rest backbone.js underscore.js

我是法国人,抱歉英语不好。我是Backbone JS的初学者。 我有一个使用codeigniter 2.1.3(使用mysql)和骨干js 1.0.0

的应用程序

我不知道为什么模型吸气剂

console.log(obj.get("x"));
仍会返回“
(an empty string)

这里是JS代码:


    var Questionnaire = Backbone.Model.extend({
        // Will contain three attributes.
        // These are their default values
        urlRoot: site_url + '/api/questionnaire/id/',
        defaults: {
            label_questionnaire: '',
            id_visuel: '',
            intro_questionnaire: '',
            version_questionnaire: '',
            type_questionnaire: ''      
        }
    });
    // Create a collection of questionnaires
    var Questionnaires = Backbone.Collection.extend({
        url: site_url + '/api/questionnaires',
        model: Questionnaire
    });

    var q =new Questionnaire({id: '7'});
    q.fetch();
    console.log(q.get("type_questionnaire"));
    console.log(q.attributes);

obj.attributes VALUES(正确):


id "7"
id_questionnaire 7
id_visuel null
intro_questionnaire ""

label_questionnaire "

my infos

" type_questionnaire 1 version_questionnaire 0

PHP API代码:


    require(APPPATH.'/libraries/REST_Controller.php');

    /*
    *   BASED ON :
    *   https://github.com/philsturgeon/codeigniter-restserver */

    class Api extends REST_Controller
    {
        public function __construct() {
            parent::__construct();
            $this->load->model('type_questionnaire_model');
        }


        function questionnaire_get() {
            if(!$this->get('id'))  
            {
                $this->response(NULL, 400);  
            }  

            $questionnaire = $this->survey_model->get_survey( $this->get('id') );  

            if($questionnaire)  
            {  
                $this->response($questionnaire, 200); // 200 being the HTTP response code  
            }else{
                $this->response(NULL, 404);  
            }  
        }
        ...
    }

数据库MYSQL表结构:


    CREATE TABLE IF NOT EXISTS `questionnaire` (
      `id_questionnaire` int(11) NOT NULL AUTO_INCREMENT,
      `label_questionnaire` text CHARACTER SET utf8 NOT NULL,
      `id_visuel` int(11) DEFAULT NULL,
      `intro_questionnaire` text CHARACTER SET utf8 NOT NULL,
      `version_questionnaire` int(11) NOT NULL,
      `type_questionnaire` int(11) NOT NULL,
      PRIMARY KEY (`id_questionnaire`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

此问题是否来自默认值?你能帮帮我吗?

0 个答案:

没有答案