ember数据如何处理表单

时间:2012-08-27 16:44:43

标签: ember-data

我正在尝试构建一个处理对象创建的示例,通过一个表单将Ember Data插入到数据库中。

到目前为止,我的商店在客户端显示结果,但就处理数据到服务器而言,似乎我的数据总是未定义并创建空白记录。

现在,我的php API没有被抛光(这就是为什么它允许空白插入)但是因为我正在运行测试,所以我只关心数据进入数据库。

我应该如何使用Ember数据处理表单? (为什么它使用'createRecords'方法而不是'createRecord'?)

所有的帮助都将令人难以置信地受到赞赏,我真的想用Ember解决问题并希望我不必放弃并切换到另一个框架。

这是我的代码,我试图通过表单和直接通过代码创建一个Person 的灰烬/ JS:

App = Em.Application.create();

App.Person = DS.Model.extend({
    id: DS.attr('string'),
    name: DS.attr('string'),
    lastname: DS.attr('string'),

    didLoad: function() { 
        console.log(this.get('lastname') + " finished loading.");
    }
}).reopenClass({ 
    url: '../api/index.php?model=entity',
});



App.adapter = DS.Adapter.create({
    findAll: function (store, type) {
        var url = type.url+'&method=read';
        $.getJSON(url, function(data) {
            store.loadMany(type, data);
        });
    },
    createRecord: function (store, type, model) {
        console.log(id);
        var url = type.url+'&method=create'

        $.ajax({
            url: url,
            data: model.get('data'),
            //data: id,
            dataType: 'json',
            type: 'POST', 
            success: function(data) {
                console.log('didCreate');

            }
        })
    },
    createRecords: function (store, type, array) {
        $.ajax({
            url: type.url+'&method=create',
            data: array.mapProperty('data'),
            dataType: 'json',
            success: function(data) {
                 store.didCreateRecords(type, array, data);
            }
        })
    },
    find: function(store, type, id) {
        console.log('find one');
        var url = type.url;
        url = url.fmt(id);
        $.getJSON(url, function(data) {
            store.load(type, id, data);
        });
    }
});




App.store = DS.Store.create({
  revision: 4,
  adapter: App.adapter
});

App.personController = Ember.ArrayController.create({
    content: [],
    init: function(){
        this._super();
        this.set('content', App.store.findAll(App.Person));
    },
    loadCode: function() {
        content: [];
        var self = this; 
        var wycats = App.store.createRecord(App.Person,  {"id":"", "name":"12", "lastname":"Steve"});
        App.store.commit();
        console.log('from file');             

    },
    submitForm: function(event) {
        console.log('Called Sumbit');
        App.store.createRecord(App.Person, this.get("content"));
        App.store.commit();
    }

});

App.personView = Ember.View.extend({
    templateName: 'text'
});

HTML:

<script type="text/x-handlebars">
<b>{{App.title}}</b><br>
{{view App.personView}}
Count: ({{App.personController.length }})
{{#each App.personController}}            
    <p>{{lastname}} {{name}}</p>
{{/each}}
</script> 
<script type="text/x-handlebars" data-template-name="text">
    <h2>Create from Code</h2>
    <div class="send_form">
            <button {{action "loadCode" target="App.personController"}}>Create</button>
        <hr>
         <h2>Create from Form</h2>
    First name: {{view Ember.TextField valueBinding="lastname"}} <br/>
    Last name:  {{view Ember.TextField valueBinding="name"}} <br/>


    <button {{action "submitForm" target="App.personController"}}>Create</button>

    <h3>Current values: {{name}} {{lastname}}</h3>

    </div>
</script>​

后端API(PHP):

<?php

    require_once 'config.php';

    if (!$errors) {

    $method = mysql_real_escape_string($_GET['method']);
    $model  = mysql_real_escape_string($_GET['model']);
    $format = mysql_real_escape_string($_GET['format']);
    if($method === ''){
        $method = mysql_real_escape_string($_POST['method']);
    }
    switch ($method) {
        case 'create' :

                $fieldlist=$vallist='';
                foreach ($_POST as $key => $value) {
                    $fieldlist.=$key.',';
                    $vallist.='\''.urlencode($value).'\','; 
                }
                $fieldlist=substr($fieldlist, 0, -1);
                $vallist=substr($vallist, 0, -1);
                $return.= $vallist.' (val)
                ';
                $return.= $fieldlist .' (field)
                ';
                $query='INSERT INTO `test_'.$model.'` ('.$fieldlist.') VALUES ('.$vallist.')'; 
                $sql = mysql_query($query) or
                    die ('Error while getting data'.mysql_error());
                $return= $sql;



            break;

        case 'read' :
                $query = "SELECT * FROM  `test_{$model}`";
                $result = mysql_query($query);
                $return = array();
                while($row = mysql_fetch_assoc($result)) {
                     $return[] = $row;
                }
                $return= json_encode($return);
            break;

        case 'reads' :
                $query = "SELECT * FROM  `test_{$model}`";
                $result = mysql_query($query);
                $return='{ "'.$model.'" : ';
                $resultados = array();
                while($row = mysql_fetch_assoc($result)) {
                     $resultados[] = $row;
                }
                $return.= json_encode($resultados);
                $return.=' }';

            break;


    }

    mysql_close();

    switch ($format) {
        default :
                echo $return;
            break;
    }
}

?>

0 个答案:

没有答案