Is this javascript code good?

I am working on a multi-tenant meeting scheduling application using Asp.net MVC and knockoutjs . I need help solving a javascript code template. I have the following script:

I get a complex ViewModel (name: TenantModel) from the server, and in javascript I use a plugin to display knockouts to create a knockout model.

Let's see what I have in my TenantModel:

TenantModel contains various complex types, such as:

  • <EmployeeModel> List
  • <CustomerModel> List
  • <ServicesModel> List
  • Profilemodel
  • and many others.

These complex types may also contain complex types, such as:

  • EmployeeModel , .
  • Model ContactModel

:

Module Pattern javascript-. javascript :

var profile = (function () {

    var _viewmodel;

    var initialize = function () {
        //initialize _viewmodel here
    };

    var bind = function (node) {
        ko.applyBindings(_viewmodel, node);
    };

    return {
        initialize: initialize,
        bind: bind
    };

})();


var employee = (function () {

    var _viewmodel;

    var initialize = function () {
        //initialize _viewmodel here
    };

    var bind = function (node) {
        ko.applyBindings(_viewmodel, node);
    };

    return {
        initialize: initialize,
        bind: bind
    };

})();


var tenant = (function () {

    var _viewmodel;

    var initialize = function (jsonTenantModel) {
        _viewmodel = ko.mapping.fromJSON(jsonTenantModel, {
            'Profile': {
                create: function (option) {
                    //create Profile using profile module
                }
            },
            'Employees': {
                create: function (option) {
                    //create Employees using Employee module
                }
            }
        })
    };

    var bind = function (node) {
        ko.applyBindings(_viewmodel, node);
    };

    return {
        initialize: initialize,
        bind: bind
    };

})();

, javascript, javascript. , ? ?

+4
1

RMP - . , . .

, .

  • ? , require.js?

  • ( )? ( ).

  • , ( ) ?

, , , , .

, - .

// Sketch of employee model
function Employee(data){
    var self = this;
    self.FirstName = ko.observable(data.FirstName);
    self.LastName = ko.observable(data.LastName);
    self.FullName = ko.computed(function(){
        return self.FirstName() + ' ' + self.LastName();
    });
}

// Sketch of a possible list of employees
// This viewmodel now will have a dependency on the Emplyee model
// So if you have for example require.js you can go:

// dataservice could be another module which you create to separate data calls 
// from your business logic

require(["models/employee", "services/dataservice"], function(employee, dataservice) {
    // and now employee model is availbale with this context:

    function EmployeesList(data){
        var self = this;
        self.employees = ko.observableArray();
        self.editDetails = function(employee){
            // code for editing employee
        }
        self.activate = function(){
            // So you can now add new employees in the 
            // collection based on your model
            ko.utils.arrayForEach(data, function(item){
                self.employees.push(new employee(item))
            });
        }
        return {
            employees: self.employees,
            activate: self.activate
        }
    }

    // a data service which might have a getEmployees method which can take options like MinAge:
    dataservice.getEmployees({ 'MinAge' : 36  }, function(employeeData){

        var myEmployeesList = new EmployeesList(employeeData)

        // You might get tired of calling activate all the time for your view modules
        // And also feel you have to deal with the area they might effect
        // If you use a SPA framework (if you are making spa) then they usually have
        // a automatic mechanism for activating modules and applying the binding e.g. Durandal.js
        myEmployeesList.activate();

        ko.applyBindings(myEmployeesList);
        // or
        ko.applyBindings(myEmployeesList, $('#employeesContainer')[0]);
    });
});

:

SPA JumpStart -

+1

Source: https://habr.com/ru/post/1525928/


All Articles