0

What I am trying to do now is trying to send data to the database via Ajax POST. I will first start with the HTML form I have,

<script type="text/x-handlebars" id="project">
    <div class="row">
        <div class="span6">
            <form class="form-horizontal">
                <div class="control-group">
                    <label class="control-label" for="projectname">Project Name: </label>

                    <div class="controls">
                        {{!view App.TextFieldEmpty}}
                        <input type="text" id="projectname" required title="First Name is Required!" pattern="[A-z ]{10,}" placeholder="Enter Project Name"/>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="projectdesc">Project Description:</label>

                    <div class="controls">
                        <textarea rows="3" id="projectdesc" placeholder="Enter Project Desc"
                                  required="Description Required"></textarea>
                    </div>
                </div>
                <div class="control-group">
                    <div class="controls">
                        <button class="btn" {{action 'createNew'}}>Add Project</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</script> 

Next, is my App.js, According to Ember guidelines, we have to map templates first right? So here it is my router:

App.Router.map(function() {

    this.resource('project');

});

Next, the table I am inserting the data into is a simple one, having just three fields; id, projectname & projectdesc.

App.Model = Ember.Object.extend({

});

App.Project = App.Model.extend({
    id : null,
    projectname : null,
    projectdesc : null
});

Now about the issue,

App.ProjectController = Ember.ArrayController.extend({
    actions : {
        createNew : function() {
            this.get('model').createNew();
        }
    }
});
    App.ProjectRoute = Ember.Route.extend({

    });


    App.Project.reopenClass({
        createNew : function() {

            dataString = $("#project").serialize();

            $.ajax({
                type : "POST",
                url : "http://ankur.local/users/createNewProject",
                data : dataString,
                dataType : "json",
                success : function(data) {
                    alert("yes");
                }
            });
        }
    });

I am getting this error on the console:

Uncaught TypeError: Object [object Array] has no method 'createNew' 

That appears to be on the line

this.get('model').createNew();

Furthermore, I am using non-RESTful PHP backend.

Even though I think I have created the method in the model. Moroever, I have used the GET exactly this way and it worked but the difference was I returned the method in the model. In this case, I thought I had to call the method from the controller. Where I might be doing wrong? I would appreciate any help or suggestions.

1 Answer 1

1

Instead of

this.get('model').createNew();

Try the below.

App.Project.createNew();
Sign up to request clarification or add additional context in comments.

2 Comments

Okay, that worked! I think data is now going but there's again a small glitch. The projectname & projectdesc are going as 0 in the database why? :(
What are you getting in your php layer when you are sending project name and description and is the id of the record getting saved as "0"??

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.