Ext.data.JsonP.soap({"title":"Using SOAP Services in Ext JS","guide":"
Contents
\n\nSOAP (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\nFor 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\nExt.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\nNext create the store, proxy and reader.
\n\nvar 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\nLet'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\nNow 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:
store.load({\n params: {\n brand: 'Blendtec'\n }\n});\n
\n\nThe 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\nLet'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\nstore.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\nNow, 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\nThis 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\nproxy: {\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\nCall 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\nCreate, 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\nThese 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\nTo issue a create request first we have to create a new record:
\n\nvar blender = Ext.create('Blender', {\n name: 'WildSide Jar',\n price: 99\n});\n
\n\nThen add the record to the store and call its sync method:
\n\nstore.add(blender);\nstore.sync();\n
\n\nThis 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\nThe 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\nWe can verify that the record has the correct id by checking its id property after the\nstore has been successfully synchronized:
\n\nstore.sync({\n success: function() {\n console.log(blender.getId()); // 3\n }\n});\n
\n\nTo update a record just modify one of it's fields, and then synchronize the store:
\n\nstore.getAt(0).set('price', 200);\nstore.sync();\n
\n\nTo destroy a record, remove it from the store and then synchronize:
\n\nstore.removeAt(1);\nstore.sync();\n
\n\nJust 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\nAnd 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"});