java - 与 AngularJS 一起使用的封装模式

标签 java javascript angularjs architecture data-modeling

我需要在 Javascript 中创建智能对象/函数以与我的 AngularJS 应用程序一起使用。我应该为此使用什么模式?我目前正在使用我研究过的通用 JavaScript“模块”模式,但我想知道我是否应该以 AngularJS 方式对这些对象/函数(如下)进行建模。也许作为“服务”?

我有 Java 背景,这让我不太愿意将带有辅助方法的对象称为“服务”。但我可能需要调整我对 JavaScript/AngularJS 的定义。

该应用程序是一个基本的评分系统。两个主要对象/函数如下:

LessonScoreCard

/* 
 * LessonScoreCard
 * 
 * Is a "module" that keeps track of the score a user has for a given lesson
 * and whether or not for a given question there are more correct answers 
 * that can be made.
 */

var lessonScoreCard = (function() {

    //Map of questions to scores
    var privateQuestionNumberToScoreMap = {};

    //API to be used by external clients
    var publicAPI = {

        //A public function utilizing privates
        setScore: function(questionNumber, score) {
            privateQuestionNumberToScoreMap[ questionNumber ] = score;
        },

        //Sum the total score
        getTotalScore: function( ){
            var totalScore = 0;
            for( var questionNumber in privateQuestionNumberToScoreMap ){
                totalScore += privateQuestionNumberToScoreMap[questionNumber];
            }
        }
    };

    return publicAPI;
})();

答案键

/* 
 * AnswerKey
 * 
 * Is a "module" that takes an answer key and performs functions
 * related to it.
 */

var answerKey = (function() {

    //Set of right answers
    var privateQuestionNumberToAnswerArrayMap;

    //API to be used by external clients
    var publicAPI = {
        setAnswerKey: function(answerKeyModel) {
            privateQuestionNumberToAnswerArrayMap = answerKeyModel;
        },
        // A public function utilizing privates
        isCorrect: function(question, answer) {
            var isCorrect = false;

            var answerArray = privateQuestionNumberToAnswerArrayMap[ question ];
            if (answerArray.indexOf(answer) !== -1) {
                isCorrect = true;
            }
            return isCorrect;
        },

        getAnswerCount: function( question ){
            var answerArray = privateQuestionNumberToAnswerArrayMap[ question ];
            return answerArray.length;
        }

    };

    return publicAPI;

})();

示例 JSON 答案键模型

    {
        1: [1, 3],
        2: [4]
    }

最佳答案

aet 是对的,但我会进一步扩展服务的作用。在 Angular 上构建应用程序时,服务和指令是您的两个主要构建 block 。

指令与 View 紧密耦合。它们用于:

  1. 更改一些数据以响应用户输入或操作
  2. 更新 UI 以响应数据变化

服务在 View 中非常解耦。它们用于:

  1. 封装业务逻辑
  2. 存储您希望在整个应用程序中共享的更强大的模型数据,尤其是当您的应用程序非常大以至于它有多个 Controller 时,或者如果您的模型数据与持久性机制紧密耦合(例如 a Backbone model )

所以服务真的可以做任何事情,只要那件事与 View 无关。我认为您应该将 lessonScoreCard 和 answerKey 模块编写为服务工厂,并将它们注入(inject)到任何需要访问其功能的指令中。

关于java - 与 AngularJS 一起使用的封装模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19370796/

相关文章:

javascript - 理解一个接一个地执行 setTimeout() 函数

javascript - Angularjs - 自定义 $resource

java - 将图形绘制从 android 移植到标准 java

java - 交换数组的最小和最大 int [Java]

java - Groovy 到 Java 代码

angularjs - 无法使用 ng new 命令创建项目

html - 删除 Angular js 中 <select> 中不必要的垂直滚动条

java - 如何在 Linux 中终止和停止在应用程序外运行的 TimerTask

javascript - 为什么我们不能在数组函数参数中省略数组破坏中的括号?

javascript - mongodb在文档更新期间使用if else设置字段值