2016-05-11 6 views
0

Ich möchte auf einige Attribute in meiner Bearbeitungsansicht zugreifen, um einige Werte zu ändern.Zugriff auf Attribute in sails.js Modell mit Javascript?

Ich habe versucht, mit etwas wie <%=course.scoreAlgo=> zugreifen, aber das funktioniert nicht.

Ich kann nicht scheinen, auf eines der Attribute in meinem Modell innerhalb des Tags script zuzugreifen. Irgendwelche und alle helfen, wie ich darauf zugreifen kann, wird sehr geschätzt!

Der Kurs Modell:

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' 
    } 
    } 
}; 

Und das ist, was der Code Ich versuche, Arbeit zu machen wie folgt aussieht:

Sie .ejs passieren
<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> 

Antwort

0

Alle Werte und angezeigt werden soll Sie müssen mit <%= %> oder <%- %> auch in <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); 
     } 
    } 
    } 
} 
+0

Aber ich wickeln f Ich setze die Attribute in '<%=%>, dann sind die Variablen nicht definiert. Dann heißt es zum Beispiel, dass 'stringArray Variable möglicherweise nicht definiert 'ist. Und das letzte Bit, das Sie kommentiert haben, soll len von sS mal 33 zum Rezitationswert hinzufügen. Ich denke ich könnte 'course.recitationScore + = len * 33'? – Gurkmeja101

+0

Ok .. Da dies Arrays sind, müssen Sie sie in JSON ändern. <% - JSON.stringify (data)%> Und Sie können Modelle in solchen Ansichten nicht aktualisieren. Sie können den Kurs nicht als JavaScript-Parameter verwenden. Wenn du es wirklich brauchst, kannst du es so machen: var course = <% - JSON.stringify (course)%>; – Bonanza

+0

Okey, danke. So kann ich nicht Modellattribute wie huh aktualisieren. Nun, das ist scheiße, dachte, es wäre so einfach :) – Gurkmeja101

Verwandte Themen