如何使用javascript访问sails.js模型中的属性

How to access attributes in sails.js model with javascript?

本文关键字:模型 属性 js sails 何使用 javascript 访问      更新时间:2023-09-26

我想访问编辑视图中的一些属性来更改一些值。

我尝试过用<%=course.scoreAlgo=>之类的东西访问,但不起作用。

我似乎无法访问script标记中模型中的任何属性。任何和所有的帮助,我可以访问这是非常感谢!

课程模式:

module.exports = {
  attributes: {
    code:{
      type: "string"
    },
    name:{
      type: "string"
    },
    recitations:{
      type: 'array',
      defaultsTo: 0
    },
    finishedAssignments:{
      type: "array"
    },
    recitationScore:{
      type: 'integer',
      defaultsTo: 0
    },
    recitationGroup:{
      type: "string",
      defaultsTo: "none"
    },
    shown:{
      type: "string",
      defaultsTo: "no"
    },
    scoreAlgo:{
      type:"array"
    },
    takes:{
      model: 'student'
    }
  }
};

这就是我试图制作的代码:

<form action="/course/update/<%= course.id %>" method="POST">
  <h2>Edit course</h2>
  <input value="<%= course.code%>" type="text" name="code"><br/>
  <input value="<%= course.name%>" type="text" name="name"><br/>
  <input value="<%= course.shown %>" type="text" name="shown"><br/>
  <input value="<%= course.recitationScore%>" type="text" name="recitationScore"><br/>
  <p>Score algo, specify by saying how many have to be completed for each "subgroup".
    If two from a, two from b and one from c means full score, put "2,2,1"</p>
  <input value="<%=course.scoreAlgo%>" type="text" name="scoreAlgo">
  <input type="button" value="Calculate recitationScore" onclick="calcScore()">
  <script>
    function calcScore(){
      var stringArray = course.finishedAssignments;
      var scoreAlgo = course.scoreAlgo;
      for(var i=0; i < stringArray.length; i++){
        var str = stringArray[i];
        var sStr = str.split(",");
        for(var j=0; j < sStr.length; j++){
          var count = scoreAlgo[i]
          var s = sStr[i];
          var sS = s.split((i+1).toString);
          if(sS.length >= count){
            var score = sS.length*33;
            course.recitationScore = course.recitationScore + score;
            console.log(score);
          }
        }
      }
    }
  </script>
  <input type="submit" value="Edit course"/>
</form>

传递给.ejs并希望显示的所有值都需要用<%= %><%- %>包装,即使在<script></script> 中也是如此

function calcScore(){
  var stringArray = <%= course.finishedAssignments %>;
  var scoreAlgo = <%= course.scoreAlgo %>;
  for(var i=0; i < stringArray.length; i++){
    var str = stringArray[i];
    var sStr = str.split(",");
    for(var j=0; j < sStr.length; j++){
      var count = scoreAlgo[i]
      var s = sStr[i];
      var sS = s.split((i+1).toString);
      if(sS.length >= count){
        var score = sS.length*33;
       //course.recitationScore = course.recitationScore + score; <!- not sure what should it do
        console.log(score);
      }
    }
  }
}