ExtJs. Simple things: how to pass extra parameters with Ext.data.Model.save

ExtJs 4.0.7 is a JS framework we use. It has its own version of MVC, that is, it has models, views and controllers. Models (Ext.data.Model) are a JS representation of underlying entities (or better, data transfer objects). But they also have methods: they can be saved and loaded.
Save method is described very briefly in ExtJs docs:

    save( options ) : Ext.data.Model
    Saves the model instance using the configured proxy.

Usually you define an AJAX proxy that sends a request to the backend REST service (or other web service). Method save() is POSTing, load() is GETting, as one might have expected. But what if one needs to pass some extra parameters to the REST call?
As it happens, it is quite simple. Method save() accepts an options parameter, which is described, also briefly, as ‘Options to pass to the proxy. Config object for Ext.data.Operation.’ Ext.data.Operation config has quite a few options, but the option we need is not documented: params. It’s an object containing parameter names and values.

So, the save() method can be enhanced like this (where model is already initialized as an instance of Ext.data.Model):

        params: {
            userId: myUserId,
            url: document.location.href
        callback: function() {
            // yey, it is saved!

Actually, you can add extra parameters to load() using the same trick: just pass them into a config object (with load(), it’s a second method parameter, because first is a model ID).


About Maryna Cherniavska

I have productively spent 10+ years in IT industry, designing, developing, building and deploying desktop and web applications, designing database structures and otherwise proving that females have a place among software developers. And this is a good place.
This entry was posted in ExtJs and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s