0

我有一个奇怪的问题,我正在尝试按sortProperties项目名称对我的模型进行排序,但它不起作用。

这是我的controller

App.Model = Ember.Object.extend({

});

//Map appropriate columns
App.Projects = App.Model.extend({
    id : null,
    projectname : null,
    projectdesc : null,
    projectbudget : null
});

App.ProjectsController = Ember.ObjectController.extend({

});

App.ProjectsController = Ember.ArrayController.extend({
    sortProperties: ['projectname'],
    sortAscending: true,
    //This function is binded to the searchText value of search box.
    filteredContent : function() {
        //'i' flag means user can search with either upper case or lower case character

        var searchText = this.get('searchText'), regex = new RegExp(searchText, 'i');

        return this.get('model').filter(function(item) {

            return regex.test(item.projectname);
        });
    }.property('searchText', 'model')
});

App.ProjectdetailController = Ember.ObjectController.extend({
    actions : {
        update : function() {
            var id = this.get('id');
            data = {
                projectname : this.get('projectname'),
                projectdesc : this.get('projectdesc'),
                projectbudget : this.get('projectbudget'),
            };
            console.log(JSON.stringify(data));
            console.log(id);
            $.ajax({
                type : "POST",
                url : "http://pioneerdev.us/users/editProject/" + id,
                data : data,
                dataType : "json",
                success : function(data) {
                    console.log('success');
                    //alert('');
                }
            });
            alertify.success("Record Updated");
            this.transitionTo('projects');
            return false;
        }
    }
});

//Model to back the Projects template
App.ProjectsRoute = Ember.Route.extend({
    model : function() {
        return App.Projects.findAll();
    }
});

//Model to ProjectDetail view
App.ProjectdetailRoute = Ember.Route.extend({
    model : function(params) {
        return App.Projects.findBy(params.project_id);
    }
});

//methods for the model
App.Projects.reopenClass({
    sortProperties: ['projectname'],
    findAll : function() {
        return new Ember.RSVP.Promise(function(resolve, reject) {
            $.getJSON("http://pioneerdev.us/users/getProjects", function(data) {
                var result = data.projects.map(function(row) {
                    return App.Projects.create(row);
                });
                resolve(result);
            }).fail(reject);
        });
    },

    findBy : function(project_id) {

        return new Ember.RSVP.Promise(function(resolve, reject) {
            var project = App.Projects.create();
            $.getJSON("http://ankur.local/users/getProjectById/" + project_id, function(data) {
                var result = project.setProperties(data.project);
                resolve(result);
            }).fail(reject);
        });
    }
});

这是模板:

<script type="text/x-handlebars" id="projects">

        <div class="row-fluid">
        <div class="span3 offset3">
            <div class="row-fluid">
                <div class="span10" id="projectlist">
                    {{input type="text" value=searchText placeholder="Search Your Projects"}}
                    <ul>
                        <li>{{#each item in filteredContent}}
                            {{#link-to 'projectdetail' item}}{{item.projectname}}{{/link-to}}
                            <br>
                            {{else}}
                            <p>Sorry, No Data</p>
                            {{/each}}
                        </li>

                    </ul>
                </div>
            </div>

            <div class="row">
                <div class="span5 offset5">{{outlet}}</div>
            </div>
        </div>

        </div>

我可以做些什么来实现我的动机?我的代码有什么问题吗?

4

1 回答 1

1

在您的filteredContent CP 中,您应该过滤array 控制器的arrangedContent 属性

http://emberjs.com/api/classes/Ember.ArrayController.html#property_arrangedContent

App.ProjectsController = Ember.ArrayController.extend({
    sortProperties: ['projectname'],
    sortAscending: true,
    //This function is binded to the searchText value of search box.
    filteredContent : function() {
        //'i' flag means user can search with either upper case or lower case character

        var searchText = this.get('searchText'), regex = new RegExp(searchText, 'i');

        return this.get('arrangedContent').filter(function(item) {

            return regex.test(item.projectname);
        });
    }.property('searchText', 'arrangedContent')
});
于 2013-11-05T08:27:52.830 回答