Angular,无法访问变量

时间:2016-11-02 20:32:44

标签: javascript angularjs salesforce

我得到了

  

错误:无法找到变量:UserEntity

文件task-calendar-controller.js

中发生了这种情况

我使用angular来制作salesforce应用。 UserEntity用于将检索到的Salesforce数据库数据转换为JavaScript。

文件结构。

enter image description here

app.js

angular

    .module("taskCalendar", ['ngRoute'])

    .constant('resourceUrl', '/resource/'+Date.now()+'/taskmanagement__taskCalendar/taskCalendar/app')

    .config(['$routeProvider', 'resourceUrl', function($routeProvider, resourceUrl) {
        $routeProvider
            .when('/', {
                templateUrl:  resourceUrl+'/task/task-calendar.html'
                ,controller: 'taskCalendarController'
            })
            .otherwise({
                redirectTo: '/'
            });
    }])

    .service('task', function() {
        return SObjectModel.Task;
    })

    .service('user', function() {
        return SObjectModel.User;
    });

用户entity.js

//Used to convert the retrieved Salesforce database data to JavaScript

var UserEntity = function() {

};

UserEntity.fromRemoteObjectModel = function(model) {
    var entity = new UserEntity();

    entity.id = model.get('Id');
    entity.firstName = model.get('FirstName');
    entity.lastName = model.get('LastName');
    entity.email = model.get('Email');

    return entity;
}

UserEntity.prototype.toRemoteObjectModelDetails = function() {
    var details = {};

    if (this.id) {
        details.Id = this.id;
    }

    details.FirstName = this.firstName;
    details.LastName = this.lastName;
    details.Email = this.email;

    return details;
}

UserEntity.prototype.id = '';
UserEntity.prototype.firstName = '';
UserEntity.prototype.lastName = '';
UserEntity.prototype.email = '';

任务日历controller.js

angular

    .module('taskCalendar')

    .controller('taskCalendarController', ['$scope', 'task', 'user', function($scope, task , user) {
        //Search box code
        var u = new user();
        $scope.users = []
        $scope.searchBox = null;

       // Get user input from search box
        $scope.change = function(text) {
            userInput = $scope.searchBox;
            console.log('searchBox = ' + userInput);

            // Make input useable for search criteria
            var elements = userInput.split(" ");
            var first = (elements[0] || "") + "%";
            var last  = (elements[1] || "") + "%";

            // Salesforce criteria to be able to search server
            var criteria = { where: {
                    FirstName: {like: first},
                    LastName:  {like: last}
                },
                orderby: [ {LastName: 'ASC'}, {FirstName: 'ASC'} ],
                limit: 10 
            };

            u.retrieve(criteria, function(error, results, event){

                if(error) {
                    alert(error.message);
                }
                $scope.$apply(function() {

                    results.forEach(function(element, index, array) {

                        // Error happens right here
                        var entity = UserEntity.fromRemoteObjectModel(element);
                        $scope.users.push(entity);
                    });
                    console.log($scope.users);
                });
            });}

}]);

任务calendar.html

 <input type="text" class="form-control" id="typeahead_example_1" name="typeahead_example_1" ng-model="searchBox" ng-change="change(text)">

1 个答案:

答案 0 :(得分:1)

您应该先在控制器中创建new instance of UserEntity,然后才能使用它。

var _userentity = new UserEntity();
_userentity.fromRemoteObjectModel(element);