2015年7月25日 星期六

transitionToRoute the same route problem


http://stackoverflow.com/questions/18624601/correct-way-to-transitiontoroute/18642910

A route with a dynamic segment will only have its model hook called when it is entered via the URL. If the route is entered through a transition (e.g. when using the link-to Handlebars helper), then a model context is already provided and the hook is not executed. Routes without dynamic segments will always execute the model hook.

sol:
http://guides.emberjs.com/v1.10.0/routing/specifying-a-routes-model/


App = Ember.Application.create();

App.Router.map(function() {
    this.resource('about');
    this.resource('calc');
    this.resource('todo');
    this.resource('phones', function() {
        this.route('viewPhone', {
            path: "/viewPhone/:userid"
        });
    });


});

var varphones = [{
    objectId: 0,
    first: 'Ryan',
    last: 'Florence',
    avatar: 'https://si0.twimg.com/profile_images/3123276865/5c069e64eb7f8e971d36a4540ed7cab2.jpeg',
    phone: '0938535359'

}, {
    objectId: 1,
    first: 'Tom',
    last: 'Dale',
    avatar: 'https://si0.twimg.com/profile_images/1317834118/avatar.png',
    phone: '0938535349'
}, {
    objectId: 2,
    first: 'Yehuda',
    last: 'Katz',
    avatar: 'https://si0.twimg.com/profile_images/3250074047/46d910af94e25187832cb4a3bc84b2b5.jpeg',
    phone: '0938535459'
}];
App.PhonesRoute = Ember.Route.extend({
    model: function() {
        return varphones;
    },
  actions: {
    invalidateModel: function() {
      Ember.Logger.log('Route is now refreshing...');
      this.refresh();
    }
  }

});

App.PhonesViewPhoneRoute = Ember.Route.extend({

    model: function(params) {
        console.log(params);
        return varphones[params.objectId];
    }

  
});

App.PhonesController = Ember.Controller.extend({
    name: '',
    phone: '',
    search: '',

    init: function() {
        this._super();
        Parse.initialize("57ky0yYlGjtL93qWuMG2rYNt1HMBlO0CLLIhKmgb", "ISFeeGTcJ8fsvTX9NOrxRkj6MhnzPkNWOgqnLUsK");
        var tool = Object.create(Tool);
        tool.showAlertDivId = 'status';
    },
    actions: {
        parseSave: function() {

            if (this.phone.length <= 0) {
                tool.failAlert();
                return;
            }
            if (this.name.length <= 0) {
                tool.failAlert();
                return;
            }
            var Phone = Parse.Object.extend("Phone");
            var phone = new Phone();
            phone.set('name', this.get('name'));
            phone.set('phone', this.get('phone'));
            phone.set('first', this.get('name'));
            phone.set('last', this.get('name'));
            phone.set('avatar', this.get('name'));


            phone.save(null, {

                success: function(object) {

                    tool.susAlert();
                },
                error: function(model, error) {

                    tool.failAlert();
                }

            });
        },
        parseList: function() {
            var tool = Object.create(Tool);
            tool.showAlertDivId = 'status';
            var Phone = Parse.Object.extend("Phone");
            var query = new Parse.Query(Phone);
            var con=this;
             query.find({
                success: function(results) {
                    varphones=results;
                    console.log("varphones:"+JSON.stringify(varphones));
                    tool.susAlert();
                    Ember.Logger.log('Controller requesting route to refresh...');
                    con.send('invalidateModel');//"Controller requesting route to refresh..."

                },
                error: function(model, error) {

                    tool.failAlert();
                    this.nowList = error;
                }
            }).then(function  () {
             
            });
        }


    }




});

沒有留言:

張貼留言