简体   繁体   中英

How can two SC.RecordArray types be concatenated in Sproutcore?

The following is the code :

        queryTree = SC.Query.local('Tree.Category',
        "categoryId = {categoryId}", {
            categoryId: this.get('guid'),
            orderBy: "name ASC"
        });
        queryNote = SC.Query.local('Tree.Note',
            "categoryId = {categoryId}", {
            categoryId: this.get('guid'),
            orderBy: "name ASC"
        });
        var arrayCategory = Tree.store.find(queryTree);
        var arrayNote = Tree.store.find(queryNote);
        //Concatenate arrayCategory to arrayNote

I want to return a new array of records that appends the results to arrayCategory and arrayNote. I went through the documentation, but there doesn't seem to be a concatenate function.

This should work just fine :

var result = Tree.store.find(SC.Query.local([Tree.Category, Tree.Note],
  "categoryId = {categoryId}", {
  categoryId: this.get('guid'),
  orderBy: "name ASC"
}));

To concatenate two array you can use the pushObjects method but it will not work with the result of an SC.Query because it return an SC.RecordArray which is not editable (because it is auto updated when you add or remove records).

I thought I would have to concatenate the two search results. But I fixed the problem this way: For every record in Tree.Note and Tree.Category I created a field called isChild and set it as YES in the former and NO in the latter. Further I changed the return value of the function to this:

return Tree.store.find(SC.Query.local(['Tree.Category','Tree.Note'],
        "categoryId = {categoryId}", {
            categoryId: this.get('guid'),
            orderBy: 'isChild, name',

        }))

EDIT : Something's still wrong. Could someone suggest how should change this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM