Search code examples
javascriptjsonxmlodatasapui5

UI5 bind new data to model from controller


I am trying to replace data in my model with an array that that i have manually done work on to filter myself. I get this data from model.read(). The problem is none of the documented ways of updating data in a model in ui5 are working or even seem to respond to being called. I am using odata v2 but all these techniques are documented to work for the v2 model: https://openui5.hana.ondemand.com/docs/topics/6c47b2b39db9404582994070ec3d57a2.html

filterFeed: function() {
    var model = this.getOwnerComponent().getModel();
    var view = this.getView();

    model.read("/assessmentFeed", {
        success: function(odata) {
            var comments = odata.results;
            var results = [];

            // ...  do work to fill 'results'

            model.setProperty('/assessmentFeed', results);

            var json = new sap.ui.model.json.JSONModel(results);
            view.setModel(json, "/assessmentFeed");

            var list = view.byId("feedList");
            list.bindAggregation("items", "/assessmentFeed", new sap.m.FeedListItem());
        }
    });
},

This is the feed list in my xml view

<layout:content>
    <m:FeedInput post="onFeedPost" class="sapUiSmallMarginTopBottom"/>
    <m:List id="feedList" showSeparators="Inner" items="{path: '/assessmentFeed', sorter: {path: 'DATE', descending: true}}">
        <m:FeedListItem sender="{MEMBERID}" timestamp="{DATE}" text="{COMMENT}" convertLinksToAnchorTags="All"/>
    </m:List>
</layout:content>

I don't get any errors but no discernible effect after calling all the above functions.

enter image description here

This is from the model's aBindings array showing that assessmentFeed is in the model's bindings from the chrome debugger.


Solution

  • Why you're trying to put your data in a JSON model and in the oData model?

    Controller:

            filterFeed: function() {
                var model = this.getOwnerComponent().getModel();
                var view = this.getView();
                var json = new sap.ui.model.json.JSONModel(); //New JSON model
    
                model.read("/assessmentFeed", {
                    success: function(odata) {
                        var comments = odata.results;
                        var results = [];
    
                        // ...  do work to fill 'results'
    
                        view.setModel(json, "JSON"); //Set json model and name it JSON                      
                        json.setProperty("/assessmentFeed", results); //Push results into your json model with path /assessmentFeed             
                    }
                });
    

    View: Binding Path is JSON>/assessmentFeed

    <layout:content>
        <m:FeedInput post="onFeedPost" class="sapUiSmallMarginTopBottom"/>
        <m:List id="feedList" showSeparators="Inner" items="{path: 'JSON>/assessmentFeed', sorter: {path: 'DATE', descending: true}}">
            <m:FeedListItem sender="{JSON>MEMBERID}" timestamp="{JSON>DATE}" text="{JSON>COMMENT}" convertLinksToAnchorTags="All"/>
        </m:List>
    </layout:content>