jQuery and ODataPerfect TogetherDavid HoersterCo-Founder, BrainCredits.comdavid@braincredits.com
About MeC# MVP (April 2011)Co-Founder of BrainCredits (braincredits.com)Senior Technical Director for Resources Global ProfessionalsPresident of Pittsburgh .NET Users GroupOrganizer of recent Pittsburgh Code CampsTwitter - @DavidHoersterBlog – http://geekswithblogs.net/DavidHoersterEmail – david@braincredits.com
AgendaQuick overview of WCF Data Services and ODataGetting data from a WCF Data ServiceFiddler, LINQPad, web appsCalling WCF Data Services and Custom Service Methods via jQueryUsing datajs as a client librarydatajs as a caching clientJSONPOptional: Other uses for your data context
WCF Data Services – What?Allows you to expose or consume data over HTTP using RESTUses the Open Data Protocol (OData)Data exposed as resources addressable as URIsData is an underlying model of some sortEntity Data Model (easiest)LINQ to SQLCustom entity classes (roll your own provider)
WCF Data Services – Why?ConsistencyAccepted format for data transport (OData)Navigate relationshipsSimple configuration in codeGrowing support for OData format
WCF Data Services – ODataOData can be exposed in one of two formatsAtom (XML)JSON (text)Data itself is exposed as one or more feedsIndividual items in a feed are entriesOData is published by Microsoft and is an open source specificationAllows other parties to create consumers and producers of OData feeds
WCF Data ServicesDemo – Getting Data from a WCF Data Service
WCF Data ServicesGetting data from a service – WRAP-UPUnderstanding the URI’sXML vs. JSON sizeJSON structureUsing jQuery to iterate through resultsGetting the next set of results
WCF Data Services w/ Web AppsYou can create custom service operationsYou can use GET or POSTs with custom service operationsSecurity still holds trueSetServiceOperationAccessRuleCustom Service Operations support returningvoidprimitiveEntityIEnumerableIQueryable
WCF Data Services w/ jQueryDEMO – Custom service methods and performing updates
Connecting WCF DS w/ jQuerySimple web app – WRAP-UPCustom service methodsInserts, updates, deletesCustom Operations honor OData keywords
DataJSRecently moved from Alpha to 1.0Available via CodePlexAlso, NuGet package (search ‘datajs’)Provides cross-browser support to connect to data sourcesCurrently just supports OData servicesProvides helpers to caching data via remote services or local storageSupports JSON and Atom formats
DataJS w/ JSONReading dataOData.read(…)“Data” returned is at that ‘d’ levelDoesn’t exist with datajs – needs to be handled with raw wcf data service callsWriting dataOData.request(…)Supports POST/PUT/MERGE/DELETEData variables don’t have to be pure JSON – datajs will formatSupports batching requests
DataJS - CachingDataJS provides a simple caching mechanismcreateCache allows you to create named cachesYou can read from themYou can clear themMore advanced functionalitySee the Netflix example from datajs site
JSONPWCF Data Services don’t support JSONP by defaultNeed JSONPDefaultBehaviorAttributeNeed also to set specific parameters in request$format$callback
Using WCF Data ServicesWith jqGridProbably easiest to set up either a WCF REST service or a MVC Action Method to retrieve the data.WCF Data Service Custom Operations expect the string arguments to be surrounded by single quotes (‘).jqGrid doesn’t send params like this.Service Operations expect eithervoid, primitive, Entity, IEnumerable, IQueryablejqGrid requires special props to be returnedrow, total, etc.
ResourcesBrainCredits Link:http://www.braincredits.com/Lesson/16854datajs site: http://datajs.codeplex.com/JSONPSupportBehavior Attribute: http://go.microsoft.com/fwlink/?LinkId=208228jqGrid site: http://www.trirand.com/blog/OData site: www.odata.org

jQuery and OData - Perfect Together

  • 1.
    jQuery and ODataPerfectTogetherDavid HoersterCo-Founder, BrainCredits.comdavid@braincredits.com
  • 2.
    About MeC# MVP(April 2011)Co-Founder of BrainCredits (braincredits.com)Senior Technical Director for Resources Global ProfessionalsPresident of Pittsburgh .NET Users GroupOrganizer of recent Pittsburgh Code CampsTwitter - @DavidHoersterBlog – http://geekswithblogs.net/DavidHoersterEmail – david@braincredits.com
  • 3.
    AgendaQuick overview ofWCF Data Services and ODataGetting data from a WCF Data ServiceFiddler, LINQPad, web appsCalling WCF Data Services and Custom Service Methods via jQueryUsing datajs as a client librarydatajs as a caching clientJSONPOptional: Other uses for your data context
  • 4.
    WCF Data Services– What?Allows you to expose or consume data over HTTP using RESTUses the Open Data Protocol (OData)Data exposed as resources addressable as URIsData is an underlying model of some sortEntity Data Model (easiest)LINQ to SQLCustom entity classes (roll your own provider)
  • 5.
    WCF Data Services– Why?ConsistencyAccepted format for data transport (OData)Navigate relationshipsSimple configuration in codeGrowing support for OData format
  • 6.
    WCF Data Services– ODataOData can be exposed in one of two formatsAtom (XML)JSON (text)Data itself is exposed as one or more feedsIndividual items in a feed are entriesOData is published by Microsoft and is an open source specificationAllows other parties to create consumers and producers of OData feeds
  • 7.
    WCF Data ServicesDemo– Getting Data from a WCF Data Service
  • 8.
    WCF Data ServicesGettingdata from a service – WRAP-UPUnderstanding the URI’sXML vs. JSON sizeJSON structureUsing jQuery to iterate through resultsGetting the next set of results
  • 9.
    WCF Data Servicesw/ Web AppsYou can create custom service operationsYou can use GET or POSTs with custom service operationsSecurity still holds trueSetServiceOperationAccessRuleCustom Service Operations support returningvoidprimitiveEntityIEnumerableIQueryable
  • 10.
    WCF Data Servicesw/ jQueryDEMO – Custom service methods and performing updates
  • 11.
    Connecting WCF DSw/ jQuerySimple web app – WRAP-UPCustom service methodsInserts, updates, deletesCustom Operations honor OData keywords
  • 12.
    DataJSRecently moved fromAlpha to 1.0Available via CodePlexAlso, NuGet package (search ‘datajs’)Provides cross-browser support to connect to data sourcesCurrently just supports OData servicesProvides helpers to caching data via remote services or local storageSupports JSON and Atom formats
  • 13.
    DataJS w/ JSONReadingdataOData.read(…)“Data” returned is at that ‘d’ levelDoesn’t exist with datajs – needs to be handled with raw wcf data service callsWriting dataOData.request(…)Supports POST/PUT/MERGE/DELETEData variables don’t have to be pure JSON – datajs will formatSupports batching requests
  • 14.
    DataJS - CachingDataJSprovides a simple caching mechanismcreateCache allows you to create named cachesYou can read from themYou can clear themMore advanced functionalitySee the Netflix example from datajs site
  • 15.
    JSONPWCF Data Servicesdon’t support JSONP by defaultNeed JSONPDefaultBehaviorAttributeNeed also to set specific parameters in request$format$callback
  • 16.
    Using WCF DataServicesWith jqGridProbably easiest to set up either a WCF REST service or a MVC Action Method to retrieve the data.WCF Data Service Custom Operations expect the string arguments to be surrounded by single quotes (‘).jqGrid doesn’t send params like this.Service Operations expect eithervoid, primitive, Entity, IEnumerable, IQueryablejqGrid requires special props to be returnedrow, total, etc.
  • 17.
    ResourcesBrainCredits Link:http://www.braincredits.com/Lesson/16854datajs site:http://datajs.codeplex.com/JSONPSupportBehavior Attribute: http://go.microsoft.com/fwlink/?LinkId=208228jqGrid site: http://www.trirand.com/blog/OData site: www.odata.org