Snippet as enable condition

I have a UI Only checkbox field called ‘Hide’ on the work experiences model.(I have this field updated to = ‘true’ on a particular row via a row action) I am trying to create a snippet which will return true when any row has had this field updated to ‘true’.  Then I am hoping to use this snippet for an enable condition to enable another field on the same model. This snippet below doesnt seem to work(I can verify x=true once I update only the top row to hide=true.  Any ideas as to what I’m doing wrong? 

$ = skuid.$;
var weModel = skuid.$M(‘WorkExperiences’);
var weList = weModel.getRows();
var x;

for (var i = 0; i < weList.length; i ++) { 

if (weList[i].Hide === true)  { 
x= true; 
break;


else{ 
console.log(‘No’); 
x=false;
}

You should be able to do this declaratively. Fileds can be enabled based on the results of UI fields (unless I am missing something). So in Skuid, in the field settings, in the “rendering” tab, you should be able to create an enable condition based on the value in your UI only field.

Hello Danny - From a snippet perspective, you are missing a return statement.  Without a return statement, skuid will evaluate your snippet to ‘undefined’ which is not equal to true.  In order to return true (or false) from the snippet, add the following as the last line of your snippet:

return x;

I actually tried this first.  However, again, it only works when I update the first row of ‘WorkExperiences’ to Hide=true. I’m not sure why this is happening. 

I tried this as before as well. However, I get - Uncaught SyntaxError: Illegal return statement

If you are getting an exception, then there is likely something wrong with the javascript code.  Can you paste provide the full JS you tried to use that included the return statement?

$ = skuid.$;var weModel = skuid.$M(‘WorkExperiences’);
var weList = weModel.getRows();
var x;

for (var i = 0; i < weList.length; i ++) { 

if (weList[i].Hide === true)  { 
x= true; 
break;


else{ 
console.log(‘No’); 
x=false;
}

return x;

also tried 

(function() {$ = skuid.$;

var weModel = skuid.$M(‘WorkExperiences’);
var weList = weModel.getRows();
var x = false;
for (var i = 0; i < weList.length; i ++) { 

if (weList[i].Hide === true)  { 
x= true; 
   


else{ 
x=false;
}
 return x; }
 

}())

and

$ = skuid.$;
var weModel = skuid.$M(‘WorkExperiences’);
var weList = weModel.getRows();
var x;
$.each(weList,function(i,row) {
if (row.Hide === true) {
x=true;
return false;

}

else{
x=false;
}
return x;

});

thanks so much for the help! I am brand new to JS and I appreciate the feedback!

Barry can attack it from the JS angle. He is smarter than me :). Declaratively, you can create a one row model on any object. Create a UI only field on that model that is a checkbox. Then the row actions in your primary model will update that UI field on the one row object to true (checked). Then base your enable condition on that one row model’s UI only checkbox.

Raymond - Appreciate the compliment but not smarter, just different skillsets :slight_smile:

Danny -

Let me circle back to Raymond’s last comment further down but first cover your JS issues.  

1) " Uncaught SyntaxError: Illegal return statement" - The first JS you sent should work.  With that said, the reason that you are likely getting the exception is because you have your JS Resource marked to “Inline” instead of “Inline (snippet)”.  The differences  are explained here.  If you copy/paste your first JS block in to an “Inline (Snippet)” and then give it a name (e.g. checkHide) and then call that snippet from your enable condition, you should no longer get the exception.  Unfortunately, that won’t make your page work the way you want…

2) Enable condition won’t “update” when “Hide” field changes on a row - The reason for this is that when using the “Snippet returns true” source type, Skuid will only ever evaluate the result once (on initial display).  As data changes in the model, the snippet will not be re-evaluated.  This is unfortunate but something Skuid has indicated they are planning on enhancing in the future.  See here for more details on that.

So, how do I achieve what I’m after you ask?  Let’s now circle back to Raymond’s last comment.

What you really want is a way to update a field and then use that field to evaluate a condition.  To do this, you need to make sure that the field itself is involved in the enable condition using a declaritive evaluation (because of the limitation noted above in #2).  To do this, Raymond’s suggestion is a perfect solution:

1) Create a new Model called “Tracker” marking it as a UI Only model.  Make sure to enable the “Create row if none exists” option.
2) Add a UI Only checkbox field called “Hide” to this model
3) In your row action, update the “Hide” field on the “Tracker” model
4) In your enable condition, choose Model Field value and then “Tracker” model, “Hide” field

Preview the page and watch the magic work :slight_smile:

Btw, There is a way to accomplish what you are after using JS as well but it involves a “Tracker” model as well or an approach that uses undocumented APIs which aren’t recommended.

That worked!! Thank you both :wink: Also, I had a feeling the way I was using my snippet was wrong because it would only be evaluated on page load (not subsequent during updates).  Would another way to achieve this be via firing off a snippet during row action (the same one that updates hide=true) that would update field mode from ‘read only’ to ‘edit’ ??

Glad it worked Danny!

Just to clarify, the problem with the evaluation wasn’t because you were using inline (instead of inline snippet) directly.  Using inline would definitely cause the code to evaluate only 1 time (on page init) but even with inline (snippet), the snippet would only be evaluated once because of the limitation with skuid currently.  The hope is that skuid adds a way to “inform” Skuid when configuration (snippet returns true) when the snippet itself needs to be re-evaluated/called.

Regarding row action and then field mode, you could approach it this way but it would not be recommended.  For starters, it’s undocumented and not officially supported.  Ideally, skuid will add a “changeMode” API to facilitate this.  That said, I have code written (as do others) that directly manipulate the mode of components, fields, etc.  and there are situations where its the only option but they are few and far between.  In your case, you can accomplish what you 100% declaritively so I’d highly recommended sticking with that route.

Excellent! Thank you both again. These tips are invaluable;)