Extjs - Stop client to append any "id" with Model instance before sending it to server via REST
Asked Answered
D

2

5

Client(Browser) is automatically appending "id" in my JSON which I am sending to server. Here is my model with proxy:

fields: ['id','title','body'],

    idProperty: 'id',  // this is the default value (for clarity)

    // clientIdProperty: 'cliendID',

     identifier: 'sequential', // to generate -1, -2 etc on the client

    proxy: {
        type: 'rest',
        //appendId: false,
        limitParam:"",
        filterParam: "",
        startParam:'',
        pageParam:'',
        url:'http://localhost:3000/posts',

        headers: {'Content-Type': "application/json" },     

        reader: {
        type: 'json',
        rootProperty:'posts'

        },
        writer: {
            type: 'json'
        }

    }

When I create a model object to send data to server via Rest, Rest fill the 'id' field with (NameOfMymodel-number).

This is code to create and send model object to server via Rest:

var UserStore = Ext.getStore('peopleStore');
var user = Ext.create('ThemeApp.model.peopleModel',{'title': "Test", 'body': "Testing" });
user.save(); //POST /users
UserStore.load();

Is there any way to stop extjs from appending such id with my data?

This is a similar kind of problem but not what I am looking. how do i prevent an extjs model/proxy from saving the empty primary id on create

Dowd answered 9/4, 2015 at 14:56 Comment(1)
[Is there any way to disable “idProperty” of Model in Extjs?][1] [1]: #29562763Herl
D
8

Just set persist to false and that's it

Ext.define('ThemeApp.model.peopleModel', {
        extend: 'Ext.data.Model',

    fields: [ {name: 'id', type: 'int', persist: false},
                  {name: 'xyz', type: 'auto'}]
    }

The 'idProperty' default value is 'id', so simply set persist: false for id property in your model. credits: ajokon (Pointed this in comments by referring to another stackoverflow question)

Dowd answered 20/4, 2015 at 9:49 Comment(0)
B
0

You can use the Ext.data.proxy.Rest property: appendId

appendId : Boolean True to automatically append the ID of a Model instance when performing a request based on that single instance. See Rest proxy intro docs for more details. Defaults to: true

inside your proxy like this

proxy: {
    type: 'rest',
    api: {
        read: 'services/test',
    },
    appendId: false, // Prevents appending the ID to the URL
    reader: {
        type: 'json',
        rootProperty: 'data',
        messageProperty: 'processMessage'
    }
}
Blondy answered 14/6 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.