Yoji SHIDARA
null+****@clear*****
Mon Oct 1 16:12:53 JST 2012
Yoji SHIDARA 2012-10-01 16:12:53 +0900 (Mon, 01 Oct 2012) New Revision: 6d40434b0474fc86ce2c45b851e5eb6570ed0474 https://github.com/groonga/gcs/commit/6d40434b0474fc86ce2c45b851e5eb6570ed0474 Log: Use ember-data to handle domain model Modified files: public/js/gcs.js views/index.jade Modified: public/js/gcs.js (+95 -92) =================================================================== --- public/js/gcs.js 2012-10-01 14:31:38 +0900 (a844add) +++ public/js/gcs.js 2012-10-01 16:12:53 +0900 (fbe86ce) @@ -1,104 +1,100 @@ var App = Ember.Application.create(); -App.store = DS.Store.create({ - revision: 4 -}); +App.Adapter = DS.Adapter.extend({ + findAll: function(store, type) { + if (type === App.Domain) { + var self = this; + $.ajax({ + type: 'GET', + url: self.configurationEndpoint, + data: { + Version: '2011-02-01', + Action: 'DescribeDomains' + }, + dataType: 'xml', + success: function(data) { + var domainStatusMembers = $(data).find('DomainStatusList > member'); + domainStatusMembers.each(function(index) { + var domainElement = $(this); + var name = domainElement.find('DomainName').text(); + var endpoint = domainElement.find('SearchService > Endpoint').text(); -App.ApplicationController = Ember.Controller.extend(); + var self = this; + $.ajax({ + type: 'GET', + url: self.configurationEndpoint, + data: { + Version: '2011-02-01', + Action: 'DescribeIndexFields', + DomainName: name + }, + dataType: 'xml', + success: function(data) { + var fieldNames = []; + var indexFields = []; + $(data).find('IndexFields > member').each(function(index) { + var field = $(this); + fieldNames.push(field.find('IndexFieldName').text()); + var name = field.find('IndexFieldName').text(); + indexFields.push({ + id: name, + name: name + }); + }); -App.ApplicationView = Ember.View.extend({ - templateName: 'application' + var domain = { + id: name, + name: name, + endpoint: endpoint, + index_fields: indexFields, + configuration_endpoint: self.configurationEndpoint + }; + store.load(type, name, domain); + } + }); + }); + } + }); + } else { + throw "Unspported model"; + } + }, + find: function(store, type, id) { + this.findAll(store, type); // Fetch all for the simplicity + } }); -App.IndexView = Ember.View.extend({ - templateName: 'index' +App.configurationEndpoint = 'http://' + location.host + '/'; + +App.store = DS.Store.create({ + revision: 4, + adapter: App.Adapter.create({ + configurationEndpoint: App.configurationEndpoint + }) }); -App.Domain = Ember.Object.extend({ - name: null, - endpoint: null, - configurationEndpoint: null, - fieldNames: [], - didLoad: false, +App.IndexField = DS.Model.extend({ + name: DS.attr('string') +}); +App.Domain = DS.Model.extend({ + name: DS.attr('string'), + endpoint: DS.attr('string'), searchEndpoint: function() { return 'http://' + this.get('endpoint') + '/2011-02-01/search'; }.property('endpoint'), - fetchFields: function() { - var self = this; - $.ajax({ - type: 'GET', - url: this.get('configurationEndpoint'), - data: { - Version: '2011-02-01', - Action: 'DescribeIndexFields', - DomainName: this.get('name') - }, - dataType: 'xml', - success: function(data) { - var fieldNames = []; - $(data).find('IndexFields > member') - .each(function(index) { - var field = $(this); - fieldNames.push(field.find('IndexFieldName').text()); - }); - self.set('fieldNames', fieldNames); - self.set('didLoad', true); - } - }); - } + indexFields: DS.hasMany('App.IndexField', {embedded: true}), + configurationEndpoint: DS.attr('string') }); -App.Domain.reopenClass({ - host: location.host, - get configurationEndpoint() { - return 'http://' + this.host + '/'; - }, - all: null, - findAll: function() { - var domains = Ember.ArrayProxy.create({ - content: Ember.A(), - didLoad: false - }); - var self = this; - $.ajax({ - type: 'GET', - url: self.configurationEndpoint, - data: { - Version: '2011-02-01', - Action: 'DescribeDomains' - }, - dataType: 'xml', - success: function(data) { - var domainStatusMembers = $(data).find('DomainStatusList > member'); - domainStatusMembers.each(function(index) { - var domainElement = $(this); - var name = domainElement.find('DomainName').text(); - var endpoint = domainElement.find('SearchService > Endpoint').text(); - var domain = App.Domain.create({ - name: name, - endpoint: endpoint, - configurationEndpoint: self.configurationEndpoint - }); - domain.fetchFields(); - domains.pushObject(domain); - }); - domains.set('didLoad', true); - } - }); - return domains; - }, - find: function(name) { - var deferred = $.Deferred(); - var domains = this.findAll(); - domains.addObserver('didLoad', function() { - var domain = domains.findProperty('name', name); - domain.addObserver('didLoad', function() { - deferred.resolve(domain); - }); - }); - return deferred.promise(); - } +App.ApplicationController = Ember.Controller.extend(); + +App.ApplicationView = Ember.View.extend({ + templateName: 'application' +}); + +App.IndexView = Ember.View.extend({ + templateName: 'index' }); App.SearchController = Ember.ArrayController.extend({ @@ -158,7 +154,9 @@ App.SearchController = Ember.ArrayController.extend({ if (!domain) { return {}; } - var returnFields = domain.fieldNames ? domain.fieldNames.join(',') : []; + var returnFields = domain.get('indexFields').map(function(field) { + return field.get('name'); + }).join(','); var params = { q: this.get('query'), size: this.get('perPage'), @@ -166,7 +164,7 @@ App.SearchController = Ember.ArrayController.extend({ 'return-fields': returnFields }; return params; - }.property('query', 'perPage', 'start', 'domain'), + }.property('query', 'perPage', 'start', 'domain', 'domain.indexFields'), reset: function() { this.set('data', null); this.set('start', 0); @@ -225,7 +223,7 @@ App.SearchFormView = Ember.View.extend({ } }); -App.domains = App.Domain.findAll(); +App.domains = App.store.findAll(App.Domain); App.IndexController = Ember.ArrayController.extend({ contentBinding: 'App.domains' @@ -245,11 +243,16 @@ App.DomainView = Ember.View.extend({ App.DomainsRoute = Ember.Route.extend({ serialize: function(router, context) { return { - domainName: context.name + domainName: context.get('name') }; }, deserialize: function(router, params) { - return App.Domain.find(params.domainName); + var domain = App.store.find(App.Domain, params.domainName); + var deferred = Ember.$.Deferred(); + domain.addObserver('isLoaded', function() { + deferred.resolve(domain); + }); + return deferred.promise(domain); } }); Modified: views/index.jade (+3 -3) =================================================================== --- views/index.jade 2012-10-01 14:31:38 +0900 (df36640) +++ views/index.jade 2012-10-01 16:12:53 +0900 (2ca6b04) @@ -38,7 +38,7 @@ html tbody tr th Configuration Endpoint - td {{App.Domain.configurationEndpoint}} + td {{App.configurationEndpoint}} script(data-template-name="domain", type="text/x-handlebars") h1 Domain '{{name}}' @@ -52,8 +52,8 @@ html th Fields td ul - {{#each fieldNames}} - li {{this}} + {{#each indexFields}} + li {{name}} {{/each}} script(data-template-name="search", type="text/x-handlebars") -------------- next part -------------- HTML����������������������������...Download