2012-12-05 70 views
3

我想讀取extjs數據存儲的特​​定值並在本地處理它。 響應XML是這樣的:當接收到響應,我想讀「BNAME」價值如何讀取extjs數據存儲

<user><name>abc</name><surname>def</surname><book><bname>book1</bname></book></user> 

我店將只有一個用戶入口。到目前爲止,我已經嘗試了兩種方法,都給出了錯誤。

approach1:

Ext.define('user', { 
    extend: 'Ext.data.Model', 
    fields: [ 'name', 'surname'], 
    hasMany: {model: 'Book', name: 'book'}, 
     proxy: { 
     type: 'rest', 
     url : request, 
     reader: { type: 'json', root: 'user'} 
    } 
}); 
Ext.define('Book', { 
    extend: 'Ext.data.Model', 
    fields: [ 'name'], 
    belongsTo: 'user' 
}); 
var userstore = Ext.create('Ext.data.Store', { 
     model: "user" 
}); 
incidentstore.load({ 
    callback: function() { 
     var inc = userstore.first(); 
     var bk = inc.getBook(); 
     console.log(dev.get('bname')); 
    } 
}); 

運行上述代碼給出錯誤 'Ext.define不是一個函數'。

Approach2:

var proxy1 = new Jx.JxHttpProxyRest({ 
    url: request, 
    api : { 
      read : { headers : {  'Accept' : APP + '.abc.def.usermanage.user+json;version=1' } } 
     } 
    }); 

var reader = new Ext.data.XmlReader({ 
    rootProperty : 'user', 
    record : 'book', 
    id : 'id', 
}, [{ name : 'bname', mapping : 'book > bname'} ]); 

var writer = new Ext.data.XmlWriter({ 
    encode : false 
}); 

var newstore = new Ext.data.Store({ 
    id : 'book', 
    restful : true, 
    proxy : proxy1, 
    remoteSort : true, 
    reader : reader, 
    writer : writer, 
    autoload: true, 
    listeners: { 
     load: function() { 
      console.log(newstore.data.first()); 
     } 
    } 
}); 
Ext.data.DataProxy.addListener('load', function(proxy, type, action, options, res) { 
    console.log(res.responseText); 
}); 
newstore.load({ 
    params : { start : 0, limit : myPageSize }, 

}); 

上面的代碼不會在控制檯上顯示任何內容。

我是新來的extjs,不知道如何從響應訪問'bname'值。這將是巨大的,如果有人能幫助

+0

您準確使用哪個版本的ExtJs? (顯然4,但哪一個)。 'Ext.define不是一個函數'可能是你在索引中沒有包含正確的Ext文件的結果 - 你能提供一個包含庫的位嗎?另一個可能的問題是'請求',你究竟在哪裏設置? – Izhaki

+0

extjs版本是3.1.1。我認爲這就是爲什麼它使Ext.define不是函數錯誤。對於我正在使用的版本,@ lontivero的代碼是否有任何解決方法? –

回答

2

下面的代碼爲我工作:

var newproxy = new Ext4.data.proxy.Rest({ 
    url : request, 
    headers : { 
     }, 
    reader : { 
     type : 'json', 
     root : 'user.book' 
    }}); 

// Typical Store collecting the Proxy, Reader and Writer together. 
var newstore = Ext4.create('Ext4.data.Store', { 
    id : 'book', 
    fields: ['bname'], 
    restful : true, // <-- This Store is RESTful 
    autoLoad : true, 
    proxy : newproxy 
}); 

var book; 
newstore.load(); 
newstore.each(function(rec) { 
    book= rec.get('bname'); 
}); 
5

在此請看:

// The user model definition 
Ext.define('user', { 
    extend: 'Ext.data.Model', 
    fields: [ 'name', 'surname', 'book' ] 
}); 

// The store instance 
var userstore = Ext.create('Ext.data.Store', { 
    model: 'user', 
    proxy: { 
     type: 'memory', 
     reader: { 
      type: 'xml', 
      record: 'user', 
      root: 'users' 
     } 
    } 
}); 

// data sample to test the approach 
mydata = 
    [ 
     ['Juan', 'Alonso', [ { bname: 'El loco' }, { bname: 'El cuerdo' } ]], 
     ['Susana', 'Cabrera', [ { bname: 'Adios a las palomas' }]] 
    ]; 

// load the store with the sample data 
userstore.loadData(mydata, false); 

// display the first book for the first record 
var firstRecord = userstore.getAt(0); 
var firstBook = firstRecord.get('book')[0]; 
alert(firstBook.bname) 

或者看到這裏的工作代碼:http://jsfiddle.net/lontivero/HTj5Q/

正如你所看到的,它工作正常。但是,如果您的商店「總是」只有一條記錄,那麼您不應該使用商店,您應該使用模型並加載模型。

UPDATE

好吧,如果你正在使用的ExtJS 3.1.1(這將是很好的前放棄如此重要的信息),你可以做到這一點如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
    <head> 
     <title>For extjs 3.1.1</title> 

     <script src="http://extjs-public.googlecode.com/svn/tags/extjs-3.1.1/release/adapter/ext/ext-base.js" type="text/javascript" ></script> 
     <script src="http://extjs-public.googlecode.com/svn/tags/extjs-3.1.1/release/ext-all.js" type="text/javascript" ></script> 
    </head> 
    <body> 
     <script type="text/javascript"> 
     Ext.onReady(function(){ 
      // The store instance 
      var userstore = new Ext.data.ArrayStore({ 
       fields: ['name', 'surname', 'book'], 
       reader: new Ext.data.ArrayReader({ 
        idIndex: 0 // id for each record will be the first element 
       }) 
      }); 

      // data sample to test the approach 
      mydata = 
       [ 
         ['Juan', 'Alonso', [ { bname: 'El loco' }, { bname: 'El cuerdo' } ]], 
         ['Susana', 'Cabrera', [ { bname: 'Adios a las palomas' }]] 
       ]; 

      // load the store with the sample data 
      userstore.loadData(mydata, false); 

      // display the first book for the first record 
      var firstRecord = userstore.getAt(0); 
      var firstBook = firstRecord.get('book')[0]; 
      alert(firstBook.bname); 
     }); 

     </script> 
    </body> 
</html> 
+0

它給出的錯誤是Ext.define不是函數。我的extjs版本是3.1.1。上述代碼對我的版本是否有任何解決方法? –

+0

我已經更新了答案。 – lontivero

+0

是我的更新有用嗎? – lontivero

0

要使Lontivero的樣本完整,使用findRecord方法在特定鍵上查找記錄也很有用。正如下面的示例所示(基於lontivero的樣本),它只會得到第一個找到的記錄。因此,如果您使用具有唯一密鑰的商店進行工作,這非常有用。

你可以看到這個樣本住在:http://jsfiddle.net/jvandemerwe/dGUGb/25/

// The user model definition 
Ext.define('user', { 
    extend: 'Ext.data.Model', 
    fields: [ 'name', 'surname', 'book' ] 
}); 

// The store instance 
var userstore = Ext.create('Ext.data.Store', { 
    model: 'user', 
    proxy: { 
     type: 'memory', 
     reader: { 
      type: 'xml', 
      record: 'user', 
      root: 'users' 
     } 
    } 
}); 

// data sample to test the approach 
mydata = 
    [ 
     ['Juan', 'Alonso', [ { bname: 'El loco' }, { bname: 'El cuerdo' } ]], 
     ['Juan', 'Lopez', [ { bname: 'Superdooper' }, { bname: 'Swinglist' } ]], 
     ['Susana', 'Cabrera', [ { bname: 'Adios a las palomas' }]] 
    ]; 

// load the store with the sample data 
userstore.loadData(mydata, false); 

// Finds the first!!! matching Record in this store by a specific field value. 
// API info: findRecord(fieldName, value, [startIndex], [anyMatch], [caseSensitive], [exactMatch]) 

var keyToFind = 'Juan'; 
var found = userstore.findRecord('name', keyToFind, 0, false, false, true); 

if (found != null) { 

    var list = ''; 
    var author = found.get('name')+' '+ found.get('surname'); 
    var books = found.get('book'); 

    Ext.each(books, function(book, idx) { 
     if (list != '') list = list + ', '; 
     list = list+book.bname; 
    }); 

    alert('Books of '+author+'\n'+list); 
} else { 
    alert('no books with key \''+keyToFind+'\''); 
} 
1

您使用分機版本4的代碼,但你的庫是3.1.1

它只是除非你升級你的庫是行不通的到版本4

0
var departmentStore = Ext.create('Ext.data.Store', { 
fields : [ 
'name', 
'active', 
'dateActive', 
'dateInactive', 
'description', 
'director', 
'numEmployees', 
{ 
name : 'id', 
type : 'int' 
} 
], 
proxy : { 
type : 'ajax', 
url : 'data.json', 
reader : { 
type : 'json', 
root : 'data', 
idProperty : 'id', 
//successProperty : 'meta.success' //??? 
} 
} 
}); 

departmentStore.load({ 
callback : function(records, operation, successful) { 
if (successful) { 
console.log('department name:', 
records[0].get('name')); 
} 
else { 
console.log('the server reported an error'); 
} 
} 
});