1 // ========================================================================== 2 // Project: SproutCore - JavaScript Application Framework 3 // Copyright: ©2006-2011 Strobe Inc. and contributors. 4 // Portions ©2008-2011 Apple Inc. All rights reserved. 5 // License: Licensed under MIT license (see license.js) 6 // ========================================================================== 7 /*globals CoreTools */ 8 9 /** 10 This DataSource connects to the SproutCore sc-server to retrieve targets 11 and tests. Currently this DataSource is read only. 12 */ 13 CoreTools.DataSource = SC.DataSource.extend({ 14 15 /** 16 Fetch a group of records from the data source. Knows how to fetch 17 a list of targets and tests. 18 */ 19 fetch: function(store, query) { 20 var ret = NO; 21 switch(query.get('recordType')) { 22 case CoreTools.Target: 23 ret = this.fetchTargets(store, query); 24 break; 25 case CoreTools.Test: 26 ret = this.fetchTests(store, query); 27 break; 28 } 29 30 return ret; 31 }, 32 33 // .......................................................... 34 // FETCHING TARGETS 35 // 36 37 /** 38 Fetch the actual targets. Only understands how to handle a remote query. 39 */ 40 fetchTargets: function(store, query) { 41 42 if (!query.get('isRemote')) return NO ; 43 44 SC.Request.getUrl(CoreTools.attachUrlPrefix('/sc/targets.json')) 45 .set('isJSON', YES) 46 .notify(this, 'fetchTargetsDidComplete', { query: query, store: store }) 47 .send(); 48 return YES ; 49 }, 50 51 fetchTargetsDidComplete: function(request, opts) { 52 var response = request.get('response'), 53 query = opts.query, 54 store = opts.store, 55 storeKeys; 56 57 if (!SC.$ok(response)) { 58 console.error("TODO: Add handler when fetching targets fails"); 59 } else { 60 storeKeys = store.loadRecords(CoreTools.Target, response); 61 store.dataSourceDidFetchQuery(query, storeKeys); 62 } 63 }, 64 65 // .......................................................... 66 // FETCHING TESTS 67 // 68 69 /** 70 Load tests for a particular URL. Only understands local querys with a 71 URL. 72 */ 73 fetchTests: function(store, query) { 74 var url = query.get('url') ; 75 76 if (!query.get('isRemote') || !url) return NO ; // not handled 77 78 SC.Request.getUrl(url) 79 .set('isJSON', YES) 80 .notify(this, 'fetchTestsDidComplete', { query: query, store: store }) 81 .send(); 82 return YES ; 83 }, 84 85 fetchTestsDidComplete: function(request, opts) { 86 var response = request.get('response'), 87 store = opts.store, 88 query = opts.query, 89 storeKeys; 90 91 if (!SC.$ok(response)) { 92 console.error("TODO: Add handler when fetching tests fails"); 93 } else { 94 storeKeys = store.loadRecords(CoreTools.Test, response); 95 store.dataSourceDidFetchQuery(query, storeKeys); // notify query loaded 96 } 97 } 98 99 }); 100