This is a migrated thread and some comments may be shown as answers.

Get Current User Id to filter feeds

1 Answer 341 Views
Show your code
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
ReadAthon
Top achievements
Rank 1
ReadAthon asked on 17 Sep 2015, 09:12 PM

Hello, 

 

I am trying to filter the feeds that i used the sample friends app, and i would like to filter it using the current user id. However, it could'nt get the value of the logged in member, how can i do that? most tries the data will be undefined 

 

/**
 * Activities view model
 */

var app = app || {};
var currentUserId;
app.Activities = (function () {
    'use strict'
//    console.log( app.Users.currentUser.get('data').Id);

    // Activities model
    var activitiesModel = (function () {

        var activityModel = {

            id: 'Id',
            fields: {
                Text: {
                    field: 'Text',
                    defaultValue: ''
                },
                CreatedAt: {
                    field: 'CreatedAt',
                    defaultValue: new Date()
                },
                Picture: {
                    fields: 'Picture',
                    defaultValue: null
                },
                UserId: {
                    field: 'UserId',
                    defaultValue: null
                },
                Likes: {
                    field: 'Likes',
                    defaultValue: []
                }
            },
            CreatedAtFormatted: function () {

                return app.helper.formatDate(this.get('CreatedAt'));
            },
            PictureUrl: function () {

                return app.helper.resolvePictureUrl(this.get('Picture'));
            },
            User: function () {

                var userId = this.get('UserId');
                console.log(userId);
                var user = $.grep(app.Users.users(), function (e) {
                    return e.Id === userId;
                })[0];

                return user ? {
                    DisplayName: user.DisplayName,
                    PictureUrl: app.helper.resolveProfilePictureUrl(user.Picture)
                } : {
                    DisplayName: 'Anonymous',
                    PictureUrl: app.helper.resolveProfilePictureUrl()
                };
            },
            isVisible: function () {
                 currentUserId = app.Users.currentUser.data.Id;
                var userId = this.get('UserId');
                return currentUserId === userId;
            }

        };
      
 
    //    var id = app.Users.currentUser.get('data.id');
   //     console.log("User", id);
   
        //app.everlive.Users.currentUser(function (data) {
        //    console.log(data.result);
        //});

  
  //      var activity = activities.getByUid(activityUid);
        // Activities data source. The Backend Services dialect of the Kendo UI DataSource component
        // supports filtering, sorting, paging, and CRUD operations.
        var activitiesDataSource = new kendo.data.DataSource({
            type: 'everlive',
            schema: {
                model: activityModel
            },
            transport: {
                // Required by Backend Services
                typeName: 'Activities'
            },
          
            change: function (e) {

                if (e.items && e.items.length > 0) {
                    $('#no-activities-span').hide();
                } else {
                    $('#no-activities-span').show();
                }
            },
            sort: { field: 'CreatedAt', dir: 'desc' },

            serverFiltering: true,

     // I should set the value of the currentUserId so the filter works.

            filter: { field: 'UserId', operator: 'eq', value: CurrentUserId }
        });

        return {
            activities: activitiesDataSource
        };

    }());

    // Activities view model
    var activitiesViewModel = (function () {

        // Navigate to activityView When some activity is selected
        var activitySelected = function (e) {

            app.mobileApp.navigate('views/activityView.html?uid=' + e.data.uid);
        }; 

        // Navigate to Profile When some Profle is selected
        var profileSelected = function (e) {

            app.mobileApp.navigate('views/Me.html?uid=' + e.data.uid);
        };

        // Navigate to app home
        var navigateHome = function () {

            app.mobileApp.navigate('#welcome');
        };

        // Logout user
        var logout = function () {

            app.helper.logout()
            .then(navigateHome, function (err) {
                app.showError(err.message);
                navigateHome();
            });
        };

        return {
            activities: activitiesModel.activities,
            me: app.Users.currentUser,
            activitySelected: activitySelected,
            logout: logout
        };

    }());

    return activitiesViewModel;

}());

1 Answer, 1 is accepted

Sort by
0
Dimitar Dimitrov
Telerik team
answered on 22 Sep 2015, 01:58 PM
Hi,

Here is the solution in order to make it work.
I have defined a function getCurrentUserId  in activites.js, than will return me the current user id. You can take it from the app.Users viewModel (users.js). The tricky part is that you have to make an additional check, if the app.Users.currentUser.data exists, because activities.js file is loaded in the index.html and at the moment of loading you may not be logged in.

Then just call that function in the filter object that you pass to the data source
filter: { field: 'UserId', operator: 'eq', value: getCurrentUserId() }

I am sending you the whole activities.js file that I have, just in case you have any issues.
/**
 * Activities view model
 */
 
var app = app || {};
 
app.Activities = (function () {
    'use strict'
    // Activities model
    var activitiesModel = (function () {
 
        var activityModel = {
 
            id: 'Id',
            fields: {
                Text: {
                    field: 'Text',
                    defaultValue: ''
                },
                CreatedAt: {
                    field: 'CreatedAt',
                    defaultValue: new Date()
                },
                Picture: {
                    fields: 'Picture',
                    defaultValue: null
                },
                UserId: {
                    field: 'UserId',
                    defaultValue: null
                },
                Likes: {
                    field: 'Likes',
                    defaultValue: []
                }
            },
            CreatedAtFormatted: function () {
 
                return app.helper.formatDate(this.get('CreatedAt'));
            },
            PictureUrl: function () {
 
                return app.helper.resolvePictureUrl(this.get('Picture'));
            },
            User: function () {
 
                var userId = this.get('UserId');
 
                var user = $.grep(app.Users.users(), function (e) {
                    return e.Id === userId;
                })[0];
 
                return user ? {
                    DisplayName: user.DisplayName,
                    PictureUrl: app.helper.resolveProfilePictureUrl(user.Picture)
                } : {
                    DisplayName: 'Anonymous',
                    PictureUrl: app.helper.resolveProfilePictureUrl()
                };
            },
            isVisible: function () {
                var currentUserId = app.Users.currentUser.data.Id;               
                var userId = this.get('UserId');
 
                return currentUserId === userId;
            }
        };
 
        // Activities data source. The Backend Services dialect of the Kendo UI DataSource component
        // supports filtering, sorting, paging, and CRUD operations.       
        var getCurrentUserId = function(){
            if(app.Users.currentUser.data)
                return app.Users.currentUser.data.Id;
        }
         
        var activitiesDataSource = new kendo.data.DataSource({
            type: 'everlive',
            schema: {
                model: activityModel
            },
            transport: {
                // Required by Backend Services
                typeName: 'Activities'
            },
            change: function (e) {
 
                if (e.items && e.items.length > 0) {
                    $('#no-activities-span').hide();
                } else {
                    $('#no-activities-span').show();
                }
            },
            sort: { field: 'CreatedAt', dir: 'desc' },
            filter: { field: 'UserId', operator: 'eq', value: getCurrentUserId() }
        });
 
        return {
            activities: activitiesDataSource
        };
 
    }());
 
    // Activities view model
    var activitiesViewModel = (function () {
 
        // Navigate to activityView When some activity is selected
        var activitySelected = function (e) {
 
            app.mobileApp.navigate('views/activityView.html?uid=' + e.data.uid);
        };
 
        // Navigate to app home
        var navigateHome = function () {
 
            app.mobileApp.navigate('#welcome');
        };
 
        // Logout user
        var logout = function () {
 
            app.helper.logout()
            .then(navigateHome, function (err) {
                app.showError(err.message);
                navigateHome();
            });
        };
 
        return {
            activities: activitiesModel.activities,
            activitySelected: activitySelected,
            logout: logout
        };
 
    }());
 
    return activitiesViewModel;
 
}());


Regards,
Dimitar Dimitrov
Telerik
 

Visit the Telerik Verified Plugins Marketplace and get the custom Cordova plugin you need, already tweaked to work seamlessly with AppBuilder.

 
Tags
Show your code
Asked by
ReadAthon
Top achievements
Rank 1
Answers by
Dimitar Dimitrov
Telerik team
Share this question
or