Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "dot" notation for object property names #4

Closed
macgyver opened this issue Mar 14, 2013 · 11 comments
Closed

Support "dot" notation for object property names #4

macgyver opened this issue Mar 14, 2013 · 11 comments
Labels

Comments

@macgyver
Copy link

This is a rad plugin! I personally think it would be cool if it also supported dot notation, e.g:

<input name="dream.as.vividly.as.you.can" value="g">

would parse the same as:

<input name="dream[as][vividly][as][you][can]" value="g">
@macek
Copy link
Owner

macek commented Mar 14, 2013

I don't know of any web framework that currently supports inputs like that, but you're more than welcome to add the feature.

If you do choose to add this feature, I think it should be done by passing options object to the plugin. For example:

$(sel).serializeObject({nesting: "dot"});

Where default options would be:

$.fn.serializeObject.defaults = {
  nesting: "brackets"
};

@macgyver
Copy link
Author

I see your point - there are probably even lots of frameworks that would break if the inputs were parsed in the way I suggest (it rules out allowing periods in your input names) - then again, those frameworks are probably not designed to work with input nested in this manner in any case.

I'll let you know if I get anywhere with this, thanks for the input!

@macek
Copy link
Owner

macek commented Mar 11, 2014

@macgyver i just wanted to follow up and let you know some things have changed that can support the dot notation now.

Please see this comment from another issue.

Also, please see the update API documentation. I've included this example to support dot notation.

$.extend(FormSerializer.patterns, {
  validate: /^[a-z][a-z0-9_]*(?:\.[a-z0-9_]+)*(?:\[\])?$/i
});

Here's a fiddle demo

@AndersMadsen
Copy link

.net framework mvc uses dots as an indicator of an object
name="a.b" would mean
someObject = {a:{b: the value of a.b }}

@macek
Copy link
Owner

macek commented Oct 30, 2014

@AndersMadsen thanks for the comment. I wasn't sure where people would prefer the dot notation.

Are you having issues getting dot notation working with your .NET project?

@AndersMadsen
Copy link

@macek I'm not working on that project right now, I'm sitting on php, however I made an issue which got merged with this one (#36) I think that because of the size of .net mvc it would make sense to turn on object creation by dots as an option :).

@macek
Copy link
Owner

macek commented Nov 4, 2014

@AndersMadsen It's not clear whether you're actually having an issue or just making a comment.

"dot" notation is supported as an "option" and is working as tested.

Please let me know if you're having trouble getting this working on your own project.

@AndersMadsen
Copy link

@macek sorry for being unclear, I was having an issue when i was using it that it did not create any object at all (if you look at the issue I linked) it probably will with the dot-notation. What I was thinking about was to submit hyphen and dots as options instead of regular expressions, but still keeping the flexible reg-ex option, like

$.extend(FormSerializer.patterns, {
  validate-notation: '.' 
});

The reason I'm suggesting a feature like this is for readability, I'm not a reg-ex shark (yet) so In my case I'll have read the reg-ex thoroughly before I understand what it does.

@p10tyr
Copy link

p10tyr commented Dec 3, 2014

.NET Razor engine creates lists using dot notation. The Razor serialization engine is JSON.NET - So I think this support would be great here because I am working on WebForms with MVC 5 knowledge. So building lists on the client with dot notation and submiting to a webform based on JSON.NET. Its no problem I can use the syntax as provided to build the object but just wanted to let you know that there is a major framework that uses dot notation. The fact you dont know about it is because people that use MVC do not need this plugin because its all extended in the framework and works like dream.

Basically retro fitting projects like I am doing.. it would be nice to stay inline with .NET notations. Because using partial post backs and all that other form crap is a nightmare and this plugin is the closest thing to emulate the Razor implemented convention.

Thanks for interesting plugin.

Basically the formatter provider does not work. THis is how I name my classes for the default .NET Serializes to deserialize to a LIST of Entities in psuedo. Basiclly on load there may be bound data but you change the values with jQuery as the user goes along.

<foreach>
 <input type="hidden" name="SomeName[<%= index %>][ID]" value="<%=index.ID %>"/>
 <input type="hidden" name="SomeName[<%= index %>][Address]" value="<%=index.Address %>" />
<end foreach>

Then in webforms its as simple as. Notice "SomeName" is the variable here not the entity type!

  [WebMethod]
  public static void MyMethod(List<Data.MyEntityMOdel>  SomeName)
  {
          int blah = SomeName.Count(); 
   }

@thanhngch
Copy link

thanhngch commented Apr 25, 2019

@macgyver i just wanted to follow up and let you know some things have changed that can support the dot notation now.

Please see this comment from another issue.

Also, please see the update API documentation. I've included this example to support dot notation.

$.extend(FormSerializer.patterns, {
  validate: /^[a-z][a-z0-9_]*(?:\.[a-z0-9_]+)*(?:\[\])?$/i
});

Here's a fiddle demo

If you're using compose dot and bracket (like friend[0].nick) Using

$.extend(FormSerializer.patterns, {
    validate: /^[a-z][a-z0-9_]*(?:(?:\.[a-z0-9_]+)|(?:\[[a-z0-9_]+\]))*(?:\[\])?$/i
});

I use it and using the library jsonform (https://jsonform.github.io/jsonform) work together.

@macgyver
Copy link
Author

Haha, ok thanks for the update ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants