Yii + Backbone获取数据

时间:2014-05-28 12:40:54

标签: javascript backbone.js yii

我需要从Yii Controller中的Backbone接收数据,但我不知道该怎么做。 我需要在我的控制器中获取它,而不是将其保存到数据库

骨干代码

  var CallForm = Backbone.Model.extend({
            defaults: {
                projectname: null,
                sll: null,
                sspn: null,
                z: null,
                results: null,
                step: null
            },
            url: './index.php',
            validate: function(attr) {
                if( !(attr.projectname && attr.sll && attr.sll) > 9 ) {
                    return "Error Occurred";
                }
            },

        });

查看

        var callView = Backbone.View.extend({
            el: '.callForm',
            events: {
                'click input.submit': 'getStatus'
            },
            getStatus: function(event){
                //for each inputs function value
                var NewProjectname = $('#projectname').val();
                var NewCall = $('#call').val();
                var NewSll = $('#sll').val();
                var NewSspn = $('#sspn').val();
                var NewZ = $('#z').val();
                var NewResults = $('#results').val();
                var NewStep = $('#step').val();
                //new model parent CallForm with value's from inputs
                var newrecord = new CallForm({
                    projectname: NewProjectname,
                    call: NewCall,
                    sll: NewSll,
                    sspn: NewSspn,
                    z: NewZ,
                    results: NewResults,
                    step: NewStep,
                    url: function() {
                        return '/callnew';
                    },
                });
                newrecord.save();
                var json = newrecord.toJSON();
                $('.test').html(JSON.stringify(json));
                return false;
            }
        });
        var callForm = new CallForm();
        var callView = new callView({
            model: callForm
        });

Yii控制器代码索引操作

public function actionIndex()
    {
        if(isset($_POST['index.php']))
        {
          echo 'You';

        }
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
        $this->render('index');
    }

2 个答案:

答案 0 :(得分:1)

如果您使用的是Yii> = 1.1.3:

$json = Yii::app()->request->getRawBody();
$yourData = CJSON::decode($json);

http://www.yiiframework.com/doc/api/1.1/CHttpRequest#getRawBody-detail

答案 1 :(得分:0)

Backbone Model在请求有效负载中发送数据。你可以这样做:

$json = file_get_contents('php://input');
$data = CJSON::decode($json);

之后,您可以使用此$ data来更新或创建来自php的新记录