Backbone.js Modell.save () - Probleme mit einer RESTful-api

Habe ich ein Modell, das ich speichern, um eine Datenbank über eine api. Mein problem ist, wenn die Aktualisierung dieses Modell. Das erste mal, dass ich etwas ändern und call-Modell.speichern Sie die Attribute übergeben bekommen als JSON an die api, die analysiert Sie und speichert Sie, ohne ein Problem. Nun, das zweite mal, dass ich eine änderung vornehmen, um das Modell in die gleiche Ansicht, Modell.speichern fügt ein Undefiniertes Objekt, um mein Modell. Die api sieht dieses Objekt aus und lehnt die PUT-Anfrage.

Warum funktioniert es mit dem ersten update und nicht die zweite?

Danke für die Hilfe.

Hier ist der code, ich hoffe Sie können es verstehen.

var LDAccount = Backbone.Model.extend({
    defaults : {
        ACC_NUM : '',
        BOOK_DATE : '',
        F_NAME : ''
    },
    template : _.template(
        "<p>Full Name</p><input data-id='F_NAME' type=text value='<%= F_NAME %>' />" +
        "<p>Book Date</p><input data-id='BOOK_DATE' type=text value='<%= BOOK_DATE %>' />"
    ),
    urlRoot : 'api/account'
});

var LDAccountView = Backbone.View.extend({
    el : '', //set this when instantiating this model in the router
    initialize : function(options) {
        this.model = options.model;
        this.model.id = options.id;
        this.model.bind('change', this.render, this);
        this.model.bind('error', this.renderError, this);
    },
    render : function() {
        $(this.el).html(this.model.template(this.model.toJSON()));
        $(this.el).append(
            '<div id="model_buttons" >' +
            '<input type=button id=reset value=Reset />' +
            '<input type=button id=save value=Save />' +
            '</div>'
        );
        return this;
    },
    events : {
        'click #save' : 'save2db'
    },
    save2db : function() {
        var $fields = $(this.el).find('input[type=text]');
        var modObj = new Object();
        $fields.each(function() {
            var $field = $(this);
            var prop = $field.attr('data-id');
            var val = $field.val();
            modObj[prop] = val;
        });
        this.model.set(modObj);
        console.log(modObj);
        this.model.save({
            success : function() {
                self.reset();
            }
        });
        return false;
    },
    renderError : function() {
        $(this.el).append('<p class=error>Account does not exist</p>');
    },
    reset : function() {
    }
});

var MainView = Backbone.View.extend({
    //code omitted since it's not relevant to the question
});

var LDRouter = Backbone.Router.extend({
    routes : {
        "account/:modl/:acc_num": "renderView" 
    },

    initialize : function() {
        this.search = new MainView({ el : '#main'});
    },

    //THIS IS THE VIEW THAT RENDERS THE MODEL
    renderView : function(modl, acc_num) {
        var self = this;
        var model = null;

        switch(modl) {
            case 'account':
                model = new LDAccount();
                break;
            //these other models were omitted for clarity
            case 'asset':
                model = new LDAsset();
                break;
            case 'info':
                model = new LDInfo();
                break;
            default:
                model = new LDAccount();
                break;
        }

        this.account = new LDAccountView({ id : acc_num, el : '#accDetail', model : model });
        this.account.reset = function() {
            self.renderView(modl,acc_num);
        };
        this.account.model.fetch();
    },

    accountInfo : function (acc_num) {
        var root = '';
        var url = window.location.href;
        var hashPos = url.indexOf('#');
        if(hashPos != -1) {
            root = url.substr(0,hashPos);
        } else {
            root = url;
        }
        window.location = root + "#account/account/" + acc_num;
    }

});

$(function() {
    var app = new LDRouter();
    Backbone.history.start();
});
Hinzufügen einige Beispiel-code um weitere Informationen, was Sie tun würden wirklich helfen.
Bitte fügen Sie den code für Modell und wie Sie es speichern.

InformationsquelleAutor froinds | 2012-01-17

Schreibe einen Kommentar