Ext.data.JsonP.soap({"title":"Using SOAP Services in Ext JS","guide":"

Using SOAP Services in Ext JS

\n
\n

Contents

\n
    \n
  1. Configuring a Store to load its records from a SOAP service
  2. \n
  3. Loading records into the store
  4. \n
  5. Cusomizing the SOAP envelope and body
  6. \n
  7. Create, update, and destroy actions
  8. \n
\n
\n\n
\n\n

SOAP (Simple Object Access Protocol) is a Web Services standard built on HTTP and XML.\nThe SOAP Proxy and Ext.data.soap.Reader provide a\nconvenient way to create a SOAP request, and load the SOAP response into a\nExt.data.Store. This guide will show you how to use the SOAP Proxy and Reader to\nload data from and save data to a fictional SOAP service that provides information about\nblenders. This guide assumes a basic knowledge of the Ext JS Data Package.\nIf you are not yet familiar with the fundamentals of the Data Package please refer to the\nData Guide.

\n\n

Configuring a Store to load its records from a SOAP service

\n\n

For starters, let's take a look at the simplest configuration required to get a\nStore up and running with SOAP data. First create a\nModel.

\n\n
Ext.define('Blender', {\n    extend: 'Ext.data.Model',\n    fields: [\n        { name: 'id', type: 'int' },\n        { name: 'name', type: 'string' },\n        { name: 'price', type: 'float' }\n    ]\n});\n
\n\n

Next create the store, proxy and reader.

\n\n
var store = Ext.create('Ext.data.Store', {\n    model: 'Blender',\n    proxy: {\n        type: 'soap',\n        url: 'BlenderService/',\n        api: {\n            create: 'CreateBlender',\n            read: 'GetBlenders',\n            update: 'UpdateBlender',\n            destroy: 'DeleteBlender'\n        },\n        soapAction: {\n            create: 'http://example.com/BlenderService/CreateBlender',\n            read: 'http://example.com/BlenderService/GetBlenders',\n            update: 'http://example.com/BlenderService/UpdateBlender',\n            destroy: 'http://example.com/BlenderService/DeleteBlender'\n        },\n        operationParam: 'operation',\n        targetNamespace: 'http://example.com/',\n        reader: {\n            type: 'soap',\n            record: 'm|Blender',\n            namespace: 'm'\n        }\n    }\n});\n
\n\n

Let's go over the configuration options we just specified. We created a Store that\nwill contain \"Blender\" model instances. We configured the Store with a SOAP proxy.\nLets review the proxy's options in a bit more detail:

\n\n\n\n\n

Loading records into the store

\n\n

Now that we have everything configured, loading data into the store is as easy as calling\nthe store's load method. Behind the scenes this will create a SOAP request to the operation\nspecified by the read property in the proxy's api configuration property, which is\n\"GetBlenders\" in our example. Let's assume that the GetBlenders SOAP operation requires\na \"brand\" parameter. We can pass the parameter directly to the store's load method, or\nif the parameter value is the same for every request we could configure it directly on the\nproxy using the extraParams config. For this example\nlet's just pass it to the store's load method:

\n\n
store.load({\n    params: {\n        brand: 'Blendtec'\n    }\n});\n
\n\n

The above call should trigger a post to http://example.com/BlenderService/?operation=GetBlenders.\nAssume that the response to the above request looks like this:

\n\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n    <soap:Body>\n        <m:GetBlendersResponse xmlns:m=\"http://example.com/\">\n            <m:Blender>\n                <m:id>1</m:id>\n                <m:name>Total Blender Classic WildSide</m:name>\n                <m:price>454.95</m:price>\n            </m:Blender>\n            <m:Blender>\n                <m:id>2</m:id>\n                <m:name>The Kitchen Mill</m:name>\n                <m:price>179.95</m:price>\n            </m:Blender>\n        </m:GetBlendersResponse>\n    </soap:Body>\n</soap:Envelope>\n
\n\n

Let's pass a callback function to the load call so we can see what the store's records\nlook like after it is loaded:

\n\n
store.load({\n    params: {\n        brand: 'Blendtec'\n    },\n    callback: function() {\n        console.log(store.getCount()); // 2 records were loaded.\n        console.log(store.getAt(0).get('name')); // get the name field of the first record.\n    }\n});\n
\n\n

Cusomizing the SOAP envelope and body

\n\n

Now, using the developer tools in your browser of choice, examine the outgoing XHR requests.\nYou should see a HTTP POST to: http://example.com/BlenderService/?operation=GetBlenders.\nNow examine the post body of this request. You should see a SOAP envelope that looks\nsomething like this (formatted for readability):

\n\n
<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n    <soap:Body>\n        <GetBlenders xmlns=\"http://example.com/\">\n            <brand>Blendtec</brand>\n        </GetBlenders>\n    </soap:Body>\n</soap:Envelope>\n
\n\n

This SOAP envelope was constructed using the envelopeTpl\ntemplate and the SOAP body was constructed using the readBodyTpl template. You may need to modify the body template if the SOAP service\nrequires a different format. You won't typically need to modify the envelope template, but\nit is cusomizable as well. These configurable templates can each be either an XTemplate instance or an array of strings to form an XTemplate. The following illustrates\nusing custom templates to change the \"soap\" envelope namespace prefix to \"s\":

\n\n
proxy: {\n    ...\n    envelopeTpl: [\n        '<?xml version=\"1.0\" encoding=\"utf-8\" ?>',\n        '<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">',\n            '{[values.bodyTpl.apply(values)]}',\n        '</s:Envelope>'\n    ],\n    readBodyTpl: [\n        '<s:Body>',\n            '<{operation} xmlns=\"{targetNamespace}\">',\n                '<tpl foreach=\"params\">',\n                    '<{$}>{.}</{$}>',\n                '</tpl>',\n            '</{operation}>',\n        '</s:Body>'\n    ]\n}\n
\n\n

Call store.load() again and you should see the post body being generated from the new\ntemplates:

\n\n
<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n    <s:Body>\n        <GetBlenders xmlns=\"http://example.com/\">\n            <brand>Blendtec</brand>\n        </GetBlenders>\n    </s:Body>\n</s:Envelope>\n
\n\n

Create, update, and destroy actions

\n\n

Create, update, and destroy requests work almost the same as read requests with the exception\nof how the SOAP body is constructed. The simple difference is this - read requests\nconstruct the SOAP body using a set of paramters, while create, update, and destroy requests\nconstruct the SOAP body using a set of records. By default the templates used to create\nthe SOAP body for create, update, and destroy requests are all the same:

\n\n
[\n    '<soap:Body>',\n        '<{operation} xmlns=\"{targetNamespace}\">',\n            '<tpl for=\"records\">',\n                '{% var recordName=values.modelName.split(\".\").pop(); %}',\n                '<{[recordName]}>',\n                    '<tpl for=\"fields\">',\n                        '<{name}>{[parent.get(values.name)]}</{name}>',\n                    '</tpl>',\n                '</{[recordName]}>',\n            '</tpl>',\n        '</{operation}>',\n    '</soap:Body>'\n]\n
\n\n

These templates can be customized using the createBodyTpl, updateBodyTpl, and\ndestroyBodyTpl configuration options as described\nin the above section on customizing the SOAP envelope and body, or the\nwriteBodyTpl configuration option can be used\nto apply the same template to all three actions.

\n\n

To issue a create request first we have to create a new record:

\n\n
var blender = Ext.create('Blender', {\n    name: 'WildSide Jar',\n    price: 99\n});\n
\n\n

Then add the record to the store and call its sync method:

\n\n
store.add(blender);\nstore.sync();\n
\n\n

This will result in an HTTP POST being issued to the endpoint url with the create operation\nparameter: http://example.com/BlenderService/?operation=CreateBlender\nIf you examine the post body of this request you will see that the newly created record has\nbeen encoded into the SOAP body:

\n\n
<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n    <soap:Body>\n        <CreateBlender xmlns=\"http://example.com/\">\n            <Blender>\n                <id>0</id>\n                <name>WildSide Jar</name>\n                <price>99</price>\n            </Blender>\n        </CreateBlender>\n    </soap:Body>\n</soap:Envelope>\n
\n\n

The response to a create request should include the record as created by the server, so\nthat the record's id can be updated on the client side. For example:

\n\n
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n    <soap:Body>\n        <m:GetBlendersResponse xmlns:m=\"http://example.com/\">\n            <m:Blender>\n                <m:id>3</m:id>\n                <m:name>WildSide Jar</m:name>\n                <m:price>99</m:price>\n            </m:Blender>\n        </m:GetBlendersResponse>\n    </soap:Body>\n</soap:Envelope>\n
\n\n

We can verify that the record has the correct id by checking its id property after the\nstore has been successfully synchronized:

\n\n
store.sync({\n    success: function() {\n        console.log(blender.getId()); // 3\n    }\n});\n
\n\n

To update a record just modify one of it's fields, and then synchronize the store:

\n\n
store.getAt(0).set('price', 200);\nstore.sync();\n
\n\n

To destroy a record, remove it from the store and then synchronize:

\n\n
store.removeAt(1);\nstore.sync();\n
\n\n

Just like create actions, if the server response to an update or destroy action includes\nthe record(s) the client side record will be updated with the data in the response.

\n\n

And that's all you need to know to get up and running with SOAP and Ext JS. For more\ndetails please refer to the API docs for the SOAP Proxy and\nReader.

\n"});