1

I'm working on the User role permissions and for that I've created a form like this:

The generated HTML for each checkbox is

<div class="checker" id="uniform-department_create">
    <span>
        <input type="checkbox" 
               class="form-control department" 
               id="department_create" 
               name="department_create" 
               value="create">
    </span>
</div>

now onSubmit I'll be calling a function that would pull the user's permissions. It looks like this:

var designationForm = {

  // All permissions must be added to this Array
  // to manipulate checkboxes
  permissions: Array('department','customer','designation'),

  ...

  get_permissions: function() {
    var allowed = Array();
    $.each(designationForm.permissions, function(index, permission) {
      allowed[permission] = Array();
      $.each($("."+permission),function(input, value) {
        if ($("#uniform-"+$(value).attr('id') + " span").hasClass('checked')) {
          allowed[permission] += "{"+$(value).attr('value')+":"+"true},";
        } else {
          allowed[permission] += "{"+$(value).attr('value')+":"+"false},";
        }
      });
      return allowed[permission];
    });
    // console.log(allowed);
    return allowed;
  },
  ...
}

what it does is basically check permissions array and iterate through all permissions if it finds a checkbox's span containing the class "Checked" it adds it to the allowed array.

if I console the result then I can see correct return but I'm unable to return the resultant array.

15
  • Sounds like you may be executing the code in an async request -- perhaps using ajax? If that is the case, then you need to return a promise that you can resolve once the logic has finished executing. Unfortunately, there is not enough code posted to see how that is being done. Commented Jul 29, 2015 at 22:10
  • nope, I'm not using the ajax, just trying to get the array returned from get_permissions function Commented Jul 29, 2015 at 22:11
  • Do you mind creating a quick plunkr that demonstrates the issue? Commented Jul 29, 2015 at 22:12
  • 1
    @freedomn-m its returning empty array Commented Jul 29, 2015 at 22:25
  • 1
    @EthanBierlein I agree - I was pointing out that the issue was not with creating a "JSON object" and referencing that doesn't help the question because we're looking at the issue not side issues to do with what the code is trying to do. I wasn't advocating that this code be sent to codereview, but asserting that this is not codereview. Commented Jul 29, 2015 at 22:43

1 Answer 1

1

if I console the result then I can see correct return but I'm unable to return the resultant array.

and

its returning empty array

Copying the code and adding some extra divs/spans into jsfiddle: jsfiddle.net/fk2x8w9x/1 and looking at the console output gives:

[department: "{create:false},{delete:false},", customer: "{create:true},", designation: Array[0]]

ie, an empty array as described, but with the values set as properties of the "array".

This is because:

  • An "array" in javascript is just like any other object and can have its own properties etc.
  • You can set properties like obj.val = but also set properties via indexer, such as obj["val"] =

This is what is happening here - properties are being added instead of array "items".

If you change lines:

var allowed = Array();
allowed[permission] = Array();

to

var allowed = {};
allowed[permission] = {};

then it may make more sense.

Sign up to request clarification or add additional context in comments.

1 Comment

Care to explain the downvote so that it can be improved? Given that this was the issue as already confirmed by the OP: "@freedomn-m you got it right"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.